Question in Prolog

about list sorting and traversal

Page 1 of 1

4 Replies - 2031 Views - Last Post: 08 April 2010 - 11:02 AM Rate Topic: -----

#1 Pierre J. Iskandar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 09

Question in Prolog

Posted 02 April 2010 - 11:22 AM

Hi,
i needed to know a way to traverse a list as follows:

we have a number X, and a list of numbers
the traversal works as follows: we take the first number in the list, sum it with the next one and check, are they equal to x? if not we move on to the next element in the list while maintaining the first head and so on, if none are equal, the traversal moves to the second element and sums it with the following one and so on.

Thanks in advance
Pierre.

Is This A Good Question/Topic? 0
  • +

Replies To: Question in Prolog

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Question in Prolog

Posted 03 April 2010 - 09:08 PM

this was a simple yet amusing problem since i havent written any prolog in months..but anyway what you're trying to do is brute force through the list and find if there exists any combination of 2 elements in the list that sum up to a certain value..
sumlist([], 0).

sumlist([X], X).

sumlist([H,R|T], X):-
	M is H+R, X =:= M.

sumlist([H,_|T], X):-
	sumlist([H|T], X).

sumlist([_,R|T], X):-
	sumlist([R|T], X).


the first 3 rules dont need any explanation i guess.. the 4th rule takes care of searching the combinations of the head with every other element in the list.. the last rule takes care of searching the combinations of the 2nd element with every other element in the list.. if you have any trouble understanding this, try tracing it on paper or you can just ask here :)..hope this helps

This post has been edited by mostyfriedman: 04 April 2010 - 02:34 PM

Was This Post Helpful? 2
  • +
  • -

#3 Pierre J. Iskandar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 19-October 09

Re: Question in Prolog

Posted 05 April 2010 - 05:28 AM

thanks bro!
weren't we hanging out the other day in the U with Hadeer and we listened to Scar Symmetry off of my ipod :D?
i think it was you :D anyway, thanks again for the reply, this will help a lot :)
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

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

Re: Question in Prolog

Posted 05 April 2010 - 01:32 PM

yep that was me, and glad i could help :)
Was This Post Helpful? 0
  • +
  • -

#5 Guest_kR*


Reputation:

Re: Question in Prolog

Posted 08 April 2010 - 11:02 AM

hey, mostfriedman, i got this zebra problem to solve with prolog and i dnt even knw where the zebra is in the facts, can u help

(a) The Englishman lives in the red house.
(B) The Spaniard owns the dog.
© Coffee is drunk in the green house.
(d) The Ukrainian drinks tea.
(e) The green house is immediately to the right (your right) of the ivory house.
(f) The winston smoker owns snails.
(g) Kools are smoked in the yellow house.
(h) Milk is drunk in the middle house.
(i) The Norwegian lives in the first house on the left.
(j) The man who smokes Chesterfields lives in the house next to the man with the fox.
(k) Kools are smoked in the house next to the house where the horse is kept.
(l) The Lucky Strike smoker drinks orange juice.
(m) The Japanese smokes Parliaments.
(n) The Norwegian lives next to the blue house.

Who owns the Zebra? Who drinks water?
Was This Post Helpful? 0

Page 1 of 1