# find the difference from elements of list in prolog

Page 1 of 1

## 1 Replies - 1425 Views - Last Post: 16 December 2010 - 11:24 PMRate 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=205647&amp;s=122e80ba57da5223c13d364a9dad5eda&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 meneltour27

• New D.I.C Head

Reputation: 2
• Posts: 40
• Joined: 30-June 09

# find the difference from elements of list in prolog

Posted 16 December 2010 - 07:32 AM

hello everyone,

i have a problem
i have a list for example [9,8,7,6,5,4,3,2,1] and i want to find the difference between each two elements for example 9-8, 8-7, 7-6, ... i have done something but it's not what i want it does this 9-8 , 7-6 , 5-4 ,.... and it stops at 2 the previous element from the end of list. that's what i 've got so far

```diff(X,Y, [X,Y|_]).
diff(X,Y, [_,_|Tail]) :- diff(Tail,Y,X),Z is X - Y,print(Z).

```

and if someone show me how to put the original list in a new list that would be great!!!

Is This A Good Question/Topic? 0

## Replies To: find the difference from elements of list in prolog

### #2 mostyfriedman

• The Algorithmi

Reputation: 729
• Posts: 4,473
• Joined: 24-October 08

## Re: find the difference from elements of list in prolog

Posted 16 December 2010 - 11:24 PM

this should do the trick

```diff([_], []).

diff([], []).

diff([H1,H2|T], [D|R]):-
D is H1-H2, diff([H2|T], R).

```