1 Replies - 1135 Views - Last Post: 16 December 2010 - 11:24 PM Rate Topic: -----

#1 meneltour27  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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!!!
thanks in advance!!!

Is This A Good Question/Topic? 0
  • +

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

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • 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).


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1