How do I get the sum of given numbers in prolog?

Viewed 28441

I'm new to prolog and I'm doing some exercises for practice. So I'm trying to get the sum of the given numbers in a list. I'm trying to use this:

my_last(X, [X]).  
my_last(X, [_|L]) :- my_last(X, L).

(from here)

as my guide. So this is my code for getting the sum:

listsum(X, []).                  
listsum(X, [H|L]):-
    X is H + listsum(X, L).

when I compile it, it says

practice.pl:3: evaluable listsum(_G139,_G140) does not exist
practice.pl:2: Singleton variables: [X]

then when I try listsum(0, [1,2,3]). it returns false.

I still don't understand much about prolog, and list and recursion in prolog.

2 Answers
Related