Suppose we wish to compute successive differences. In J:
succ_diff =: 2 & (-~/\)
i.e., quite easy with dyadic
infix, though this
acts strangely: it applies -~/
to each infix (which is an array) of length 2.
To think of how this works, consider 2 & ([\)
:
2 & ([\) (i.10)
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
i.e. one can imagine building up a 9 2
array and then folding each row.
This turns out to be not quite how it works under the hood; 2 f/\ y
is an
idiom
that is recognized.
In Haskell:
λ:> let succDiff xs = zipWith (-) (tail xs) xs
This is more direct; we use (lazy) linked lists.