Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
sum_of( _ , 0 , [] ) . % nothing adds up to nothing. sum_of( [X|Xs] , S , [X|Ys] ) :- % otherwise... S > 0 , % - if the target sum S is positive, X =< S , % - and the head of the list is less than or equal to the target sum S1 is S-X , % - remove that amount from the target sum, and sum_of(Xs,S1,Ys) . % - recurse down with the new target sum sum_of( [_|Xs] , S , Ys ) :- % then, on backtracking... S > 0 , % - assuming that the target sum is positive, sum_of(Xs,S,Ys). % - recurse down again, discarding the head of the list sum_of_n_elements(Xs,N,S,Ys) :- length(Ys,N), sum_of(Xs,S,Ys) . sum_of_3_elements(Xs,S,Ys) :- sum_of_n_elements(Xs,3,S,Ys).