/* Maxium differene between adjacent elements (Rosetta code) in Picat. http://rosettacode.org/wiki/Maximum_difference_between_adjacent_elements_of_list """ Find maximum difference between adjacent elements of list. The list may have negative values, zero values, real numbers. List = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3] Output would be (which could have more verbiage): 1,8 ==> 7 2,9 ==> 7 10,3 ==> 7 """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import v3_utils. % import cp. import util. main => go. go => L = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3], Diffs = findall([Diff,A,B], (nextto(A,B,L), Diff = abs(A-B))), MaxDiff = max([Diff : [Diff,_,_] in Diffs]), println(maxdiff=MaxDiff), println(MaxDiff=[[A,B] : [Diff,A,B] in Diffs, Diff == MaxDiff]), nl. go2 => L = [1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3], Diffs = [[abs(A-B),A,B] : {A,B} in zip(L.butlast,L.tail)], MaxDiff = max([Diff : [Diff,_,_] in Diffs]), println(maxdiff=MaxDiff), println(MaxDiff=[[A,B] : [Diff,A,B] in Diffs, Diff == MaxDiff]), nl. % Returns all element of L but the last butlast(L) = L[1..L.len-1].