Skip to content

Instantly share code, notes, and snippets.

@avipars
Created July 1, 2024 17:13
Show Gist options
  • Save avipars/cc385edd15567ad06ea8a82271d1dc95 to your computer and use it in GitHub Desktop.
Save avipars/cc385edd15567ad06ea8a82271d1dc95 to your computer and use it in GitHub Desktop.
Prolog Backtracking and Cuts
% tail length
len([],A,A). %base
len([_|T],A,R) :-
A1 is A + 1,
len(T, A1, R).
% params to send
len(L,Res):- len(L,0, Res).
%tail sum up entries to list
sum_list([],A,A). % end case
sum_list([H|T],A,R) :-
A1 is A + H,
sum_list(T, A1, R).
% for humans
sum_list(L,Res):- sum_list(L,0, Res).
sum2(0,0). % base case
sum2(Num,Sum):-
Num > 0,
Num1 is Num // 10,
sum2(Num1,Sum1),
Sum is Num mod 10 + Sum1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment