How to resolve the algorithm Forward difference step by step in the Elixir programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Forward difference step by step in the Elixir 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 Elixir programming language

Source code in the elixir programming language

defmodule Diff do
  def forward(list,i\\1) do
    forward(list,[],i)
  end
  
  def forward([_],diffs,1), do: IO.inspect diffs
  def forward([_],diffs,i), do: forward(diffs,[],i-1)
  def forward([val1,val2|vals],diffs,i) do
    forward([val2|vals],diffs++[val2-val1],i)
  end
end

Enum.each(1..9, fn i ->
  Diff.forward([90, 47, 58, 29, 22, 32, 55, 5, 55, 73],i)
end)


  

You may also check:How to resolve the algorithm Text processing/1 step by step in the Lua programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Base64 decode data step by step in the C programming language
You may also check:How to resolve the algorithm Left factorials step by step in the Prolog programming language
You may also check:How to resolve the algorithm List rooted trees step by step in the Ring programming language