#!/usr/bin/env setl
--
-- Forward difference in SETL.
--
-- 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
--
--
-- This SETL program was created by Hakan Kjellerstrand (hakank@bonetmail.com)
-- Also see my SETL page: http://www.hakank.org/setl/
--
x := [90, 47, 58, 29, 22, 32, 55, 5, 55, 73];
print(all_diffs(x));
for i in [1..#x-1] loop
print("i:", i, "d:", d:=diff_i(x,i), "#d",#d);
end loop;
--
-- all forward differences
--
procedure all_diffs(x);
d := [x];
while (x := diff(x)) /= [] loop
d with:= x;
end loop;
return d;
end procedure;
--
-- The i'th forward difference list of x
--
procedure diff_i(x,i);
d := [x];
j := 1;
while j <= i and (x := diff(x)) /= [] loop
d := x;
j +:= 1;
end loop;
return d;
end procedure;
--
-- different list of x
--
procedure diff(x);
return [ x(i)-x(i-1) : i in [2..#x] ];
end procedure;