# Question in Prolog

Page 1 of 1

## 4 Replies - 2184 Views - Last Post: 08 April 2010 - 11:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=165771&amp;s=6ab0bf3025886cc307d4fbcaf67aadc8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Pierre J. Iskandar

Reputation: 0
• 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.

Pierre.

Is This A Good Question/Topic? 0

## Replies To: Question in Prolog

### #2 mostyfriedman

• The Algorithmi

Reputation: 728
• 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

### #3 Pierre J. Iskandar

Reputation: 0
• 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 ?
i think it was you anyway, thanks again for the reply, this will help a lot

### #4 mostyfriedman

• The Algorithmi

Reputation: 728
• 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

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.
( 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?