Task

Provide code that produces a list of numbers which is the   nth  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 fewer element as a result.

The second-order forward difference of   A   will be:

tdefmodule Diff do
	def forward(arr,i\\1) do
		forward(arr,[],i)
	end

	def forward([_|[]],diffs,i) do
		if i == 1 do
			IO.inspect diffs
		else 
			forward(diffs,[],i-1)
		end
	end

	def forward([val1|[val2|vals]],diffs,i) do
		forward([val2|vals],diffs++[val2-val1],i) 
	end
end 

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.

For a more formal description, see the related   Mathworld article.



Algorithmic options

 Iterate through all previous forward differences and re-calculate a new array each time.
 Use this formula (from Wikipedia):


 




Δ

n


[
f
]
(
x
)
=

∑

k
=
0


n





(


n
k


)



(
−
1

)

n
−
k


f
(
x
+
k
)


{\displaystyle \Delta ^{n}[f](x)=\sum _{k=0}^{n}{n \choose k}(-1)^{n-k}f(x+k)}




 (Pascal's Triangle   may be useful for this option.)


To add a fraction to some fraction things:
Allocate memory for a fraction thing.
Put the fraction into the fraction thing's fraction.
Append the fraction thing to the fraction things.
 
To create some fraction things:
Add 90-1/2 to the fraction things.
Add 47/1 to the fraction things.
Add 58/1 to the fraction things.
Add 29/1 to the fraction things.
Add 22/1 to the fraction things.
Add 32/1 to the fraction things.
Add 55/1 to the fraction things.
Add 5/1 to the fraction things.
Add 55/1 to the fraction things.
Add 73-1/2 to the fraction things.
 
To find the difference of some fraction things:
If the fraction things' count is less than 2, exit.
Get a fraction thing from the fraction things.
Loop.
If the fraction thing's next is nil, remove the fraction thing from the fraction things; destroy the fraction thing; exit.
Put the fraction thing's next's fraction minus the fraction thing's fraction into the fraction thing's fraction.
Put the fraction thing's next into the fraction thing.
Repeat.
 
To find the difference of some fraction things given an order:
If a counter is past the order, exit.
Find the difference of the fraction things.
Repeat.
 
A fraction thing is a thing with a fraction.
 
An order is a number.
 
To run:
Start up.
Create some fraction things.
Write "Original list:" on the console.
Show the fraction things.
Find the difference of the fraction things given 4.
Write "Order 4 forward difference:" on the console.
Show the fraction things.
Destroy the fraction things.
Wait for the escape key.
Shut down.
 
To show some fraction things:
Get a fraction thing from the fraction things.
Loop.
If the fraction thing is nil, write "" on the console; exit.
Write "" then the fraction thing's fraction on the console without advancing.
If the fraction thing's next is not nil, write ", " on the console without advancing.
Put the fraction thing's next into the fraction thing.
Repeat.