New to prolog, trying to understand how a list works in prolog.
I am trying to write a clause (what is a clause in this context?) sumList(List, sum) which returns the sum of the numbers in a list. an example would help a lot in seeing what is a clause?
PrologSum of a prolog list
Page 1 of 1
4 Replies - 967 Views - Last Post: 04 April 2009 - 06:23 PM
Replies To: Prolog
#2
Re: Prolog
Posted 04 April 2009 - 02:39 PM
you mean a predicate??, a predicate is basically a boolean function, it only returns true or false..
#3
Re: Prolog
Posted 04 April 2009 - 02:45 PM
this is a simple problem, i'd show you the solution but you have to show some effort first..i'll give you a hint though, a list consists of a head and a tail right??, so the sum of the elements in the list will be the head + the elements in the tail..actually that was the solution, if you can figure out the base case and translate this to syntax then problem is solved
This post has been edited by mostyfriedman: 04 April 2009 - 02:46 PM
#4
Re: Prolog
Posted 04 April 2009 - 02:54 PM
sumlist([],0).
sumlist([H|T],N) :- sumlist(T,N1), N is N1+H.
how close am I?
sumlist([H|T],N) :- sumlist(T,N1), N is N1+H.
how close am I?
#5
Re: Prolog
Posted 04 April 2009 - 06:23 PM
not close, you are there
...you got the solution
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|