find the difference from elements of list in prolog

Page 1 of 1

1 Replies - 1436 Views - Last Post: 16 December 2010 - 11:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=205647&amp;s=954a55d7dc7251f690b7877ece7c8b38&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 meneltour27

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

```