I want to sum all list elements greater than some given number. Here's the description:
sumup(L, N, GREATN, GEN)
sums up the members of listL
which are greater thanGREATN
to a variableN
and puts these members into the listGEN
.Sample query:
?- sumup([8, 6, 10, 3, 9, 12], N, 7, GEN). GEN = [8, 10, 9, 12], % expected answer N = 39. % 8+10+9+12 = 39
Following is my code:
sum_list([], 0).
sum_list([H|T], Sum) :-
H > 3,
sum_list(T, Rest),
Sum is H + Rest.
sum_list([H|T], Sum) :-
H < 3,
write('').
I've tried the recursive way but I failed. How can I fix it?