/*
Forward difference in Picat.
From http://rosettacode.org/wiki/Forward_difference
"""
Provide code that produces a list of numbers which is the n-th order
forward difference, given a non-negative integer (specifying the order)
and a list of numbers. The first-order forward difference of a list of
numbers (A) is a new list (B) where Bn = An+1 - An. List B should have
one less element as a result. The second-order forward difference of
A will be the same as the first-order forward difference of B. That
new list will have two fewer elements than A and one less than B.
The goal of this task is to repeat this process up to the desired order.
"""
Also, see http://mathworld.wolfram.com/ForwardDifference.html
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
main => go.
go =>
L = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73],
Diff = diff(L),
writeln(Diff),
foreach(I in 1..L.length-1)
writeln([d=I,diffi(L,I)])
end,
println(alldiffs(L)),
nl.
diff(L) = Diff =>
Diff = [L[I]-L[I-1] : I in 2..L.length].
diffi(L,D) = Diff =>
Diff1 = L,
foreach(_I in 1..D)
Diff1 := diff(Diff1)
end,
Diff = Diff1.
alldiffs(L) = Diffs =>
Diffs1 = [],
Diff = L,
foreach(_I in 1..L.length-1)
Diff := diff(Diff),
Diffs1 := Diffs1 ++ [Diff]
end,
Diffs = Diffs1.