Prolog

Sum of a prolog list

Page 1 of 1

4 Replies - 1273 Views - Last Post: 04 April 2009 - 06:23 PM Rate Topic: -----

#1 chetah  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 17-November 07

Prolog

Post icon  Posted 03 April 2009 - 04:25 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Prolog

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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..
Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

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

Was This Post Helpful? 0
  • +
  • -

#4 chetah  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 17-November 07

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?
Was This Post Helpful? 0
  • +
  • -

#5 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Prolog

Posted 04 April 2009 - 06:23 PM

not close, you are there :)...you got the solution
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1