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.

## 4 Replies - 2314 Views - Last Post: 08 April 2010 - 11:02 AM

##
**Replies To:** Question in Prolog

### #2

## 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..

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

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

## 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

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

## Re: Question in Prolog

Posted 05 April 2010 - 01:32 PM

yep that was me, and glad i could help

### #5 Guest_kR*

## 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?

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

Page 1 of 1