How to resolve the algorithm Forward difference step by step in the Picat programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Forward difference step by step in the Picat programming language
Table of Contents
Problem Statement
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: 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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Forward difference step by step in the Picat programming language
Source code in the picat programming language
go =>
L = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73],
foreach(I in 1..L.length-1)
println([d=I,diffi(L,I)])
end,
nl,
% All differences (a sublist to save space)
println(alldiffs(L[1..6])),
nl.
% Difference of the list
diff(L) = Diff =>
Diff = [L[I]-L[I-1] : I in 2..L.length].
% The i'th list difference
diffi(L,D) = Diff =>
Diff1 = L,
foreach(_I in 1..D)
Diff1 := diff(Diff1)
end,
Diff = Diff1.
% all differences
alldiffs(L) = Diffs =>
Diffs1 = [],
Diff = L,
foreach(_I in 1..L.length-1)
Diff := diff(Diff),
Diffs1 := Diffs1 ++ [Diff]
end,
Diffs = Diffs1.
You may also check:How to resolve the algorithm CRC-32 step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Raku programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the PL/I programming language
You may also check:How to resolve the algorithm 100 prisoners step by step in the Maple programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Yorick programming language