Created
July 1, 2024 17:13
-
-
Save avipars/cc385edd15567ad06ea8a82271d1dc95 to your computer and use it in GitHub Desktop.
Prolog Backtracking and Cuts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% 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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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