Forward difference: Difference between revisions
Content added Content deleted
m (→{{header|Picat}}: Added {{out}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 50: | Line 50: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V dif = s -> enumerate(s[1..]).map2((i, x) -> x - @s[i]) |
||
F difn(s, n) -> [Int] |
F difn(s, n) -> [Int] |
||
R I n != 0 {difn(dif(s), n - 1)} E s |
R I n != 0 {difn(dif(s), n - 1)} E s |
||
Line 57: | Line 57: | ||
L(i) 10 |
L(i) 10 |
||
print(difn(s, i))</ |
print(difn(s, i))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 74: | Line 74: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; |
||
with Ada.Float_Text_Io; use Ada.Float_Text_Io; |
with Ada.Float_Text_Io; use Ada.Float_Text_Io; |
||
with Ada.containers.Vectors; |
with Ada.containers.Vectors; |
||
Line 123: | Line 123: | ||
Print(Diff(A, 10)); |
Print(Diff(A, 10)); |
||
print(Diff(A, 0)); |
print(Diff(A, 0)); |
||
end Forward_Difference;</ |
end Forward_Difference;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 138: | Line 138: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
||
< |
<syntaxhighlight lang="algol68">main:( |
||
MODE LISTREAL = [1:0]REAL; |
MODE LISTREAL = [1:0]REAL; |
||
Line 158: | Line 158: | ||
printf((list fmt,s,$";"l$)) |
printf((list fmt,s,$";"l$)) |
||
OD |
OD |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 174: | Line 174: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% calculate forward differences % |
% calculate forward differences % |
||
Line 204: | Line 204: | ||
for i := 1 until length do v( i ) := diff( i ) |
for i := 1 until length do v( i ) := diff( i ) |
||
end for_order |
end for_order |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 221: | Line 221: | ||
{{works with|Dyalog APL}}{{trans|J}} |
{{works with|Dyalog APL}}{{trans|J}} |
||
< |
<syntaxhighlight lang="apl"> list ← 90 47 58 29 22 32 55 5 55 73 |
||
fd ← {⍺=0:⍵⋄(⍺-1)∇(1↓⍵)-(¯1↓⍵)} |
fd ← {⍺=0:⍵⋄(⍺-1)∇(1↓⍵)-(¯1↓⍵)} |
||
Line 229: | Line 229: | ||
2 fd list |
2 fd list |
||
54 ¯40 22 17 13 ¯73 100 ¯32</ |
54 ¯40 22 17 13 ¯73 100 ¯32</syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
< |
<syntaxhighlight lang="applescript">-- forwardDifference :: Num a => [a] -> [a] |
||
on forwardDifference(xs) |
on forwardDifference(xs) |
||
zipWith(my subtract, xs, rest of xs) |
zipWith(my subtract, xs, rest of xs) |
||
Line 453: | Line 453: | ||
return lst |
return lst |
||
end tell |
end tell |
||
end zipWith</ |
end zipWith</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>0 -> [90, 47, 58, 29, 22, 32, 55, 5, 55, 73] |
<pre>0 -> [90, 47, 58, 29, 22, 32, 55, 5, 55, 73] |
||
Line 468: | Line 468: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">; element-wise subtraction of two blocks. e.g. |
||
; vsub [1 2 3] [1 2 3] ; [0 0 0] |
; vsub [1 2 3] [1 2 3] ; [0 0 0] |
||
vsub: function [u v][ |
vsub: function [u v][ |
||
Line 482: | Line 482: | ||
print differences .order: 4 [90.5 47 58 29 22 32 55 5 55 73.5] |
print differences .order: 4 [90.5 47 58 29 22 32 55 5 55 73.5] |
||
print differences [1 2 3 4 5 6 7]</ |
print differences [1 2 3 4 5 6 7]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 491: | Line 491: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276470.html#276470 forum] |
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276470.html#276470 forum] |
||
< |
<syntaxhighlight lang="autohotkey">MsgBox % diff("2,3,4,3",1) |
||
MsgBox % diff("2,3,4,3",2) |
MsgBox % diff("2,3,4,3",2) |
||
MsgBox % diff("2,3,4,3",3) |
MsgBox % diff("2,3,4,3",3) |
||
Line 507: | Line 507: | ||
} |
} |
||
Return list |
Return list |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
BEGIN { |
BEGIN { |
||
if (p<1) {p = 1}; |
if (p<1) {p = 1}; |
||
Line 530: | Line 530: | ||
{ |
{ |
||
print diff($0, p); |
print diff($0, p); |
||
}</ |
}</syntaxhighlight> |
||
Using Pascal's triangle: |
Using Pascal's triangle: |
||
< |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
BEGIN { |
BEGIN { |
||
if (p<1) {p = 1}; |
if (p<1) {p = 1}; |
||
Line 560: | Line 560: | ||
{ |
{ |
||
print diff($0, p); |
print diff($0, p); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 584: | Line 584: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM A(9) |
||
A() = 90.0, 47.0, 58.0, 29.0, 22.0, 32.0, 55.0, 5.0, 55.0, 73.0 |
A() = 90.0, 47.0, 58.0, 29.0, 22.0, 32.0, 55.0, 5.0, 55.0, 73.0 |
||
PRINT "Original array: " FNshowarray(A()) |
PRINT "Original array: " FNshowarray(A()) |
||
Line 613: | Line 613: | ||
a$ += STR$(a(i%)) + ", " |
a$ += STR$(a(i%)) + ", " |
||
NEXT |
NEXT |
||
= LEFT$(LEFT$(a$))</ |
= LEFT$(LEFT$(a$))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 627: | Line 627: | ||
Left argument is the order, right argument is the array. |
Left argument is the order, right argument is the array. |
||
< |
<syntaxhighlight lang="bqn">FDiff ← {{-˜´˘2↕𝕩}⍟𝕨 𝕩} |
||
•Show 1 FDiff 90‿47‿58‿29‿22‿32‿55‿5‿55‿73 |
•Show 1 FDiff 90‿47‿58‿29‿22‿32‿55‿5‿55‿73 |
||
•Show 2 FDiff 90‿47‿58‿29‿22‿32‿55‿5‿55‿73</ |
•Show 2 FDiff 90‿47‿58‿29‿22‿32‿55‿5‿55‿73</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 671: | Line 671: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Use method with Pascal triangle, binomial coefficients are pre-computed |
Use method with Pascal triangle, binomial coefficients are pre-computed |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int* binomCoeff(int n) { |
int* binomCoeff(int n) { |
||
Line 708: | Line 708: | ||
printf("\n"); |
printf("\n"); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 738: | Line 738: | ||
} while ((sequence = ForwardDifference(sequence)).Any()); |
} while ((sequence = ForwardDifference(sequence)).Any()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>90, 47, 58, 29, 22, 32, 55, 5, 55, 73 |
<pre>90, 47, 58, 29, 22, 32, 55, 5, 55, 73 |
||
Line 757: | Line 757: | ||
which is then called several times for calculating n-th order forward difference. |
which is then called several times for calculating n-th order forward difference. |
||
No error checking is implemented. |
No error checking is implemented. |
||
< |
<syntaxhighlight lang="cpp">#include <vector> |
||
#include <iterator> |
#include <iterator> |
||
#include <algorithm> |
#include <algorithm> |
||
Line 867: | Line 867: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 881: | Line 881: | ||
===Using Standard Template Library=== |
===Using Standard Template Library=== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <numeric> |
#include <numeric> |
||
Line 898: | Line 898: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 913: | Line 913: | ||
Usually one will not want the intermediate results, |
Usually one will not want the intermediate results, |
||
in which case the following is sufficient: |
in which case the following is sufficient: |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <numeric> |
#include <numeric> |
||
Line 927: | Line 927: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 934: | Line 934: | ||
===Using Pascal's Triangle=== |
===Using Pascal's Triangle=== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <algorithm> |
#include <algorithm> |
||
Line 949: | Line 949: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 956: | Line 956: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(defn fwd-diff [nums order] |
||
(nth (iterate #(map - (next %) %) nums) order))</ |
(nth (iterate #(map - (next %) %) nums) order))</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
forward_difference = (arr, n) -> |
forward_difference = (arr, n) -> |
||
# Find the n-th order forward difference for arr using |
# Find the n-th order forward difference for arr using |
||
Line 972: | Line 972: | ||
for n in [0..arr.length] |
for n in [0..arr.length] |
||
console.log n, forward_difference arr, n |
console.log n, forward_difference arr, n |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>> coffee forward_difference.coffee |
<pre>> coffee forward_difference.coffee |
||
Line 987: | Line 987: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun forward-difference (list) |
||
(mapcar #'- (rest list) list)) |
(mapcar #'- (rest list) list)) |
||
Line 993: | Line 993: | ||
(setf list (copy-list list)) |
(setf list (copy-list list)) |
||
(loop repeat n do (map-into list #'- (rest list) list)) |
(loop repeat n do (map-into list #'- (rest list) list)) |
||
(subseq list 0 (- (length list) n)))</ |
(subseq list 0 (- (length list) n)))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
===Basic Version=== |
===Basic Version=== |
||
< |
<syntaxhighlight lang="d">T[] forwardDifference(T)(in T[] data, in int level) pure nothrow |
||
in { |
in { |
||
assert(level >= 0 && level < data.length); |
assert(level >= 0 && level < data.length); |
||
Line 1,015: | Line 1,015: | ||
foreach (immutable level; 0 .. data.length) |
foreach (immutable level; 0 .. data.length) |
||
forwardDifference(data, level).writeln; |
forwardDifference(data, level).writeln; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[90.5, 47, 58, 29, 22, 32, 55, 5, 55, 73.5] |
<pre>[90.5, 47, 58, 29, 22, 32, 55, 5, 55, 73.5] |
||
Line 1,030: | Line 1,030: | ||
===Alternative Version=== |
===Alternative Version=== |
||
Same output. |
Same output. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.array; |
||
auto forwardDifference(Range)(Range d, in int level) pure { |
auto forwardDifference(Range)(Range d, in int level) pure { |
||
Line 1,042: | Line 1,042: | ||
foreach (immutable level; 0 .. data.length) |
foreach (immutable level; 0 .. data.length) |
||
forwardDifference(data, level).writeln; |
forwardDifference(data, level).writeln; |
||
}</ |
}</syntaxhighlight> |
||
===Using Vector Operations=== |
===Using Vector Operations=== |
||
forwardDifference mutates the array in-place (same output): |
forwardDifference mutates the array in-place (same output): |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
T[] forwardDifference(T)(T[] s, in int n) pure nothrow @nogc { |
T[] forwardDifference(T)(T[] s, in int n) pure nothrow @nogc { |
||
Line 1,057: | Line 1,057: | ||
foreach (immutable level; 0 .. A.length) |
foreach (immutable level; 0 .. A.length) |
||
forwardDifference(A.dup, level).writeln; |
forwardDifference(A.dup, level).writeln; |
||
}</ |
}</syntaxhighlight> |
||
===Short Functional Version=== |
===Short Functional Version=== |
||
Same output: |
Same output: |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.range; |
import std.stdio, std.range; |
||
Line 1,070: | Line 1,070: | ||
a[n - 1][0 .. $ - 1])[0 .. $] }(D) |
a[n - 1][0 .. $ - 1])[0 .. $] }(D) |
||
.take(D.length)); |
.take(D.length)); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
< |
<syntaxhighlight lang="dart">List forwardDifference(List _list) { |
||
for (int i = _list.length - 1; i > 0; i--) { |
for (int i = _list.length - 1; i > 0; i--) { |
||
_list[i] = _list[i] - _list[i - 1]; |
_list[i] = _list[i] - _list[i - 1]; |
||
Line 1,089: | Line 1,089: | ||
print(_list); |
print(_list); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,110: | Line 1,110: | ||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">pragma.enable("accumulator") |
||
/** Single step. */ |
/** Single step. */ |
||
def forwardDifference(seq :List) { |
def forwardDifference(seq :List) { |
||
Line 1,141: | Line 1,141: | ||
> require(r1 == nthForwardDifference2(sampleData, n)) |
> require(r1 == nthForwardDifference2(sampleData, n)) |
||
> println(r1) |
> println(r1) |
||
> }</ |
> }</syntaxhighlight> |
||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
Using the built-in function '''iterate''' which, given a function f and n, returns the function f°f°f....°f . |
Using the built-in function '''iterate''' which, given a function f and n, returns the function f°f°f....°f . |
||
< |
<syntaxhighlight lang="lisp"> |
||
(define (Δ-1 list) |
(define (Δ-1 list) |
||
(for/list ([x (cdr list)] [y list]) (- x y))) |
(for/list ([x (cdr list)] [y list]) (- x y))) |
||
Line 1,153: | Line 1,153: | ||
((Δ-n 9) '(90 47 58 29 22 32 55 5 55 73)) |
((Δ-n 9) '(90 47 58 29 22 32 55 5 55 73)) |
||
→ (-2921) |
→ (-2921) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Diff do |
||
def forward(list,i\\1) do |
def forward(list,i\\1) do |
||
forward(list,[],i) |
forward(list,[],i) |
||
Line 1,170: | Line 1,170: | ||
Enum.each(1..9, fn i -> |
Enum.each(1..9, fn i -> |
||
Diff.forward([90, 47, 58, 29, 22, 32, 55, 5, 55, 73],i) |
Diff.forward([90, 47, 58, 29, 22, 32, 55, 5, 55, 73],i) |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,186: | Line 1,186: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">-module(forward_difference). |
||
-export([difference/1, difference/2]). |
-export([difference/1, difference/2]). |
||
Line 1,203: | Line 1,203: | ||
io:format("Initial: ~p~n",[?TEST_DATA]), |
io:format("Initial: ~p~n",[?TEST_DATA]), |
||
[io:format("~3b: ~p~n",[N,difference(?TEST_DATA,N)]) || N <- lists:seq(0,length(?TEST_DATA))], |
[io:format("~3b: ~p~n",[N,difference(?TEST_DATA,N)]) || N <- lists:seq(0,length(?TEST_DATA))], |
||
ok.</ |
ok.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>80> forward_difference:task(). |
<pre>80> forward_difference:task(). |
||
Line 1,223: | Line 1,223: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Straightforward recursive solution |
Straightforward recursive solution |
||
< |
<syntaxhighlight lang="fsharp">let rec ForwardDifference input n = |
||
match n with |
match n with |
||
| _ when input = [] || n < 0 -> [] // If there's no more input, just return an empty list |
| _ when input = [] || n < 0 -> [] // If there's no more input, just return an empty list |
||
Line 1,231: | Line 1,231: | ||
|> Seq.zip input // tupled with itself |
|> Seq.zip input // tupled with itself |
||
|> Seq.map (fun (a, b) -> b-a) // Subtract the i'th element from the (i+1)'th |
|> Seq.map (fun (a, b) -> b-a) // Subtract the i'th element from the (i+1)'th |
||
|> Seq.toList) (n-1) // Make into a list and do an n-1 difference on it</ |
|> Seq.toList) (n-1) // Make into a list and do an n-1 difference on it</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: kernel math math.vectors sequences ; |
||
IN: rosetacode |
IN: rosetacode |
||
Line 1,243: | Line 1,243: | ||
dup 0 <= |
dup 0 <= |
||
[ drop ] [ [ 1-order ] times ] if ; |
[ drop ] [ [ 1-order ] times ] if ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
( scratchpad ) { 90.5 47 58 29 22 32 55 5 55 73.5 } 4 n-order . |
( scratchpad ) { 90.5 47 58 29 22 32 55 5 55 73.5 } 4 n-order . |
||
{ 156.5 -67 1 -82 259 -304.5 } |
{ 156.5 -67 1 -82 259 -304.5 } |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: forward-difference |
||
dup 0 |
dup 0 |
||
?do |
?do |
||
Line 1,262: | Line 1,262: | ||
: test a 10 9 forward-difference bounds ?do i ? loop ; |
: test a 10 9 forward-difference bounds ?do i ? loop ; |
||
test</ |
test</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">MODULE DIFFERENCE |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
Line 1,285: | Line 1,285: | ||
WRITE (*,*) b(1:arraysize-n) |
WRITE (*,*) b(1:arraysize-n) |
||
END SUBROUTINE Fdiff |
END SUBROUTINE Fdiff |
||
END MODULE DIFFERENCE</ |
END MODULE DIFFERENCE</syntaxhighlight> |
||
< |
<syntaxhighlight lang="fortran">PROGRAM TEST |
||
USE DIFFERENCE |
USE DIFFERENCE |
||
Line 1,299: | Line 1,299: | ||
END DO |
END DO |
||
END PROGRAM TEST</ |
END PROGRAM TEST</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,314: | Line 1,314: | ||
=={{Header|FreeBASIC}}== |
=={{Header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">function forward_difference( a() as uinteger ) as uinteger ptr |
||
dim as uinteger ptr b = allocate( sizeof(uinteger) * (ubound(a)-1) ) |
dim as uinteger ptr b = allocate( sizeof(uinteger) * (ubound(a)-1) ) |
||
for i as uinteger = 0 to ubound(a)-1 |
for i as uinteger = 0 to ubound(a)-1 |
||
Line 1,331: | Line 1,331: | ||
print *(b+i) |
print *(b+i) |
||
next i |
next i |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,351: | Line 1,351: | ||
} |
} |
||
return a[:len(a)-ord] |
return a[:len(a)-ord] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,360: | Line 1,360: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">forwardDifference :: Num a => [a] -> [a] |
||
forwardDifference = tail >>= zipWith (-) |
forwardDifference = tail >>= zipWith (-) |
||
Line 1,369: | Line 1,369: | ||
main = |
main = |
||
mapM_ print $ |
mapM_ print $ |
||
take 10 (iterate forwardDifference [90, 47, 58, 29, 22, 32, 55, 5, 55, 73])</ |
take 10 (iterate forwardDifference [90, 47, 58, 29, 22, 32, 55, 5, 55, 73])</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>[90,47,58,29,22,32,55,5,55,73] |
<pre>[90,47,58,29,22,32,55,5,55,73] |
||
Line 1,383: | Line 1,383: | ||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">REAL :: n=10, list(n) |
||
list = ( 90, 47, 58, 29, 22, 32, 55, 5, 55, 73 ) |
list = ( 90, 47, 58, 29, 22, 32, 55, 5, 55, 73 ) |
||
Line 1,394: | Line 1,394: | ||
ENDDO |
ENDDO |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 90 47 58 29 22 32 55 5 55 73 |
<pre>0 90 47 58 29 22 32 55 5 55 73 |
||
Line 1,408: | Line 1,408: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main(A) # Compute all forward difference orders for argument list |
||
every order := 1 to (*A-1) do showList(order, fdiff(A, order)) |
every order := 1 to (*A-1) do showList(order, fdiff(A, order)) |
||
end |
end |
||
Line 1,424: | Line 1,424: | ||
every writes(!L," ") |
every writes(!L," ") |
||
write() |
write() |
||
end</ |
end</syntaxhighlight> |
||
{{out|A sample run}} |
{{out|A sample run}} |
||
Line 1,444: | Line 1,444: | ||
Standard IDL library function <tt>TS_diff(X,k,[/double])</tt>: |
Standard IDL library function <tt>TS_diff(X,k,[/double])</tt>: |
||
< |
<syntaxhighlight lang="idl">print,(x = randomu(seed,8)*100) |
||
15.1473 58.0953 82.7465 16.8637 97.7182 59.7856 17.7699 74.9154 |
15.1473 58.0953 82.7465 16.8637 97.7182 59.7856 17.7699 74.9154 |
||
print,ts_diff(x,1) |
print,ts_diff(x,1) |
||
Line 1,451: | Line 1,451: | ||
-18.2967 -90.5341 146.737 -118.787 -4.08316 99.1613 0.000000 0.000000 |
-18.2967 -90.5341 146.737 -118.787 -4.08316 99.1613 0.000000 0.000000 |
||
print,ts_diff(x,3) |
print,ts_diff(x,3) |
||
72.2374 -237.271 265.524 -114.704 -103.244 0.000000 0.000000 0.000000</ |
72.2374 -237.271 265.524 -114.704 -103.244 0.000000 0.000000 0.000000</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Of the many ways to code this in J, a particularly concise solution is: |
Of the many ways to code this in J, a particularly concise solution is: |
||
< |
<syntaxhighlight lang="j">fd=: 2&(-~/\)</syntaxhighlight> |
||
Alternatively, to reduce the number of J primitives, use: |
Alternatively, to reduce the number of J primitives, use: |
||
< |
<syntaxhighlight lang="j">fd=: }. - }: ^:</syntaxhighlight> |
||
(which is also elegant, because the open-ended power conjunction reads like "to the power of anything"). |
(which is also elegant, because the open-ended power conjunction reads like "to the power of anything"). |
||
For example: |
For example: |
||
< |
<syntaxhighlight lang="j"> list=: 90 47 58 29 22 32 55 5 55 73 NB. Some numbers |
||
1 fd list |
1 fd list |
||
Line 1,469: | Line 1,469: | ||
2 fd list |
2 fd list |
||
54 _40 22 17 13 _73 100 _32</ |
54 _40 22 17 13 _73 100 _32</syntaxhighlight> |
||
J is array oriented, so you can even ask for more than one forward difference at a time (i.e. <tt>N</tt> can be a list, instead of a single number): |
J is array oriented, so you can even ask for more than one forward difference at a time (i.e. <tt>N</tt> can be a list, instead of a single number): |
||
< |
<syntaxhighlight lang="j"> 1 2 3 fd list NB. First, second, and third forward differences (simultaneously) |
||
43 _11 29 7 _10 _23 50 _50 _18 |
43 _11 29 7 _10 _23 50 _50 _18 |
||
54 _40 22 17 13 _73 100 _32 0 |
54 _40 22 17 13 _73 100 _32 0 |
||
Line 1,488: | Line 1,488: | ||
1017 _1904 0 0 0 0 0 0 0 0 |
1017 _1904 0 0 0 0 0 0 0 0 |
||
2921 0 0 0 0 0 0 0 0 0 |
2921 0 0 0 0 0 0 0 0 0 |
||
0 0 0 0 0 0 0 0 0 0</ |
0 0 0 0 0 0 0 0 0 0</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
public class FD { |
public class FD { |
||
public static void main(String args[]) { |
public static void main(String args[]) { |
||
Line 1,518: | Line 1,518: | ||
return a; |
return a; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,533: | Line 1,533: | ||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,689: | Line 1,689: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,705: | Line 1,705: | ||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
< |
<syntaxhighlight lang="jq"># If n is a non-negative number and if input is |
||
# a (possibly empty) array of numbers, |
# a (possibly empty) array of numbers, |
||
# emit an array, even if the input list is too short: |
# emit an array, even if the input list is too short: |
||
Line 1,712: | Line 1,712: | ||
elif n == 1 then . as $in | [range(1;length) | $in[.] - $in[.-1]] |
elif n == 1 then . as $in | [range(1;length) | $in[.] - $in[.-1]] |
||
else ndiff(1) | ndiff(n-1) |
else ndiff(1) | ndiff(n-1) |
||
end;</ |
end;</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">def s: [90, 47, 58, 29, 22, 32, 55, 5, 55, 73]; |
||
range(0;12) as $i | (s|ndiff($i))</ |
range(0;12) as $i | (s|ndiff($i))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -c -n -f forward-difference.jq |
||
[90,47,58,29,22,32,55,5,55,73] |
[90,47,58,29,22,32,55,5,55,73] |
||
[-43,11,-29,-7,10,23,-50,50,18] |
[-43,11,-29,-7,10,23,-50,50,18] |
||
Line 1,730: | Line 1,730: | ||
[-2921] |
[-2921] |
||
[] |
[] |
||
[]</ |
[]</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
Line 1,736: | Line 1,736: | ||
Using the built-in <code>diff</code> function, which returns the 1st forward difference: |
Using the built-in <code>diff</code> function, which returns the 1st forward difference: |
||
< |
<syntaxhighlight lang="julia">ndiff(A::Array, n::Integer) = n < 1 ? A : diff(ndiff(A, n-1)) |
||
s = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73] |
s = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73] |
||
println.(collect(ndiff(s, i) for i in 0:9))</ |
println.(collect(ndiff(s, i) for i in 0:9))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,754: | Line 1,754: | ||
=={{header|K4}}== |
=={{header|K4}}== |
||
<lang |
<syntaxhighlight lang="k4">fd:1_-':</syntaxhighlight> |
||
To compute the ''n''th forward difference, call as: |
To compute the ''n''th forward difference, call as: |
||
<lang |
<syntaxhighlight lang="k4">n fd/</syntaxhighlight> |
||
In order to obtain all intermediate results, call as: |
In order to obtain all intermediate results, call as: |
||
<lang |
<syntaxhighlight lang="k4">n fd\</syntaxhighlight> |
||
{{out|Examples}} |
{{out|Examples}} |
||
Line 1,771: | Line 1,771: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun forwardDifference(ia: IntArray, order: Int): IntArray { |
fun forwardDifference(ia: IntArray, order: Int): IntArray { |
||
Line 1,806: | Line 1,806: | ||
printArray(fd) |
printArray(fd) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,824: | Line 1,824: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{def fdiff |
{def fdiff |
||
{lambda {:l} |
{lambda {:l} |
||
Line 1,852: | Line 1,852: | ||
[1017,-1904] |
[1017,-1904] |
||
[-2921] |
[-2921] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">#!/usr/bin/lasso9 |
||
define forwardDiff(values, order::integer=1) => { |
define forwardDiff(values, order::integer=1) => { |
||
Line 1,870: | Line 1,870: | ||
local(data = (:90, 47, 58, 29, 22, 32, 55, 5, 55, 73)) |
local(data = (:90, 47, 58, 29, 22, 32, 55, 5, 55, 73)) |
||
with x in generateSeries(0, #data->size-1) |
with x in generateSeries(0, #data->size-1) |
||
do stdoutnl(#x + ': ' + forwardDiff(#data, #x))</ |
do stdoutnl(#x + ': ' + forwardDiff(#data, #x))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0: array(90, 47, 58, 29, 22, 32, 55, 5, 55, 73) |
<pre>0: array(90, 47, 58, 29, 22, 32, 55, 5, 55, 73) |
||
Line 1,884: | Line 1,884: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
< |
<syntaxhighlight lang="logo">to fwd.diff :l |
||
if empty? :l [output []] |
if empty? :l [output []] |
||
if empty? bf :l [output []] |
if empty? bf :l [output []] |
||
Line 1,895: | Line 1,895: | ||
show nth.fwd.diff 9 [90 47 58 29 22 32 55 5 55 73] |
show nth.fwd.diff 9 [90 47 58 29 22 32 55 5 55 73] |
||
[-2921]</ |
[-2921]</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function dif(a, b, ...) |
||
if(b) then return b-a, dif(b, ...) end |
if(b) then return b-a, dif(b, ...) end |
||
end |
end |
||
function dift(t) return {dif(unpack(t))} end |
function dift(t) return {dif(unpack(t))} end |
||
print(unpack(dift{1,3,6,10,15}))</ |
print(unpack(dift{1,3,6,10,15}))</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
Function Diff(a()) get an array by value (a shallow copy) |
Function Diff(a()) get an array by value (a shallow copy) |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Form 80, 40 |
Form 80, 40 |
||
Module Forward_difference { |
Module Forward_difference { |
||
Line 1,928: | Line 1,928: | ||
Forward_difference |
Forward_difference |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll"> |
<pre style="height:30ex;overflow:scroll"> |
||
Line 1,945: | Line 1,945: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Built-in function: |
Built-in function: |
||
< |
<syntaxhighlight lang="mathematica">i={3,5,12,1,6,19,6,2,4,9}; |
||
Differences[i]</ |
Differences[i]</syntaxhighlight> |
||
{{out|gives back}} |
{{out|gives back}} |
||
< |
<syntaxhighlight lang="mathematica">{2, 7, -11, 5, 13, -13, -4, 2, 5}</syntaxhighlight> |
||
The n<sup>th</sup> difference can be done as follows: |
The n<sup>th</sup> difference can be done as follows: |
||
< |
<syntaxhighlight lang="mathematica">i={3,5,12,1,6,19,6,2,4,9}; |
||
Differences[i,n]</ |
Differences[i,n]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
Line 1,958: | Line 1,958: | ||
X is the list of numbers, |
X is the list of numbers, |
||
n is the order of the forward difference. |
n is the order of the forward difference. |
||
< |
<syntaxhighlight lang="matlab">Y = diff(X,n);</syntaxhighlight> |
||
{{out}} 1st order forward difference. |
{{out}} 1st order forward difference. |
||
Line 1,968: | Line 1,968: | ||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">ldiff(u, n) := block([m: length(u)], for j thru n do u: makelist(u[i + 1] - u[i], i, 1, m - j), u);</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx************************************************************* |
||
* Forward differences |
* Forward differences |
||
* 18.08.2012 Walter Pachl derived from Rexx |
* 18.08.2012 Walter Pachl derived from Rexx |
||
Line 2,004: | Line 2,004: | ||
Say n ol |
Say n ol |
||
End |
End |
||
End</ |
End</syntaxhighlight> |
||
Output is the same as for Rexx |
Output is the same as for Rexx |
||
=={{header|Nial}}== |
=={{header|Nial}}== |
||
Define forward difference for order 1 |
Define forward difference for order 1 |
||
< |
<syntaxhighlight lang="nial">fd is - [rest, front]</syntaxhighlight> |
||
{{out}} forward difference of 4th order |
{{out}} forward difference of 4th order |
||
< |
<syntaxhighlight lang="nial">b := 90 47 58 29 22 32 55 5 55 73 |
||
4 fold fd b |
4 fold fd b |
||
= 156 -67 1 -82 259 -305</ |
= 156 -67 1 -82 259 -305</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc dif(s: seq[int]): seq[int] = |
||
result = newSeq[int](s.len-1) |
result = newSeq[int](s.len-1) |
||
for i in 0..<s.high: |
for i in 0..<s.high: |
||
Line 2,029: | Line 2,029: | ||
echo difn(s, 0) |
echo difn(s, 0) |
||
echo difn(s, 1) |
echo difn(s, 1) |
||
echo difn(s, 2)</ |
echo difn(s, 2)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>@[90, 47, 58, 29, 22, 32, 55, 5, 55, 73] |
<pre>@[90, 47, 58, 29, 22, 32, 55, 5, 55, 73] |
||
Line 2,037: | Line 2,037: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="objeck"> |
||
bundle Default { |
bundle Default { |
||
class Test { |
class Test { |
||
Line 2,074: | Line 2,074: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let rec forward_difference = function |
||
a :: (b :: _ as xs) -> |
a :: (b :: _ as xs) -> |
||
b - a :: forward_difference xs |
b - a :: forward_difference xs |
||
Line 2,088: | Line 2,088: | ||
xs |
xs |
||
else |
else |
||
nth_forward_difference (pred n) (forward_difference xs)</ |
nth_forward_difference (pred n) (forward_difference xs)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,098: | Line 2,098: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: forwardDiff(l) l right(l size 1 -) l zipWith(#-) ; |
||
: forwardDiffN(n, l) l #[ forwardDiff dup println ] times(n) ;</ |
: forwardDiffN(n, l) l #[ forwardDiff dup println ] times(n) ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,116: | Line 2,116: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">fd(v)=vector(#v-1,i,v[i+1]-v[i]);</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">Program ForwardDifferenceDemo(output); |
||
procedure fowardDifference(list: array of integer); |
procedure fowardDifference(list: array of integer); |
||
Line 2,144: | Line 2,144: | ||
begin |
begin |
||
fowardDifference(a); |
fowardDifference(a); |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>:> ./ForwardDifference |
<pre>:> ./ForwardDifference |
||
Line 2,159: | Line 2,159: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub dif { |
||
my @s = @_; |
my @s = @_; |
||
map { $s[$_+1] - $s[$_] } 0 .. $#s-1 |
map { $s[$_+1] - $s[$_] } 0 .. $#s-1 |
||
Line 2,165: | Line 2,165: | ||
@a = qw<90 47 58 29 22 32 55 5 55 73>; |
@a = qw<90 47 58 29 22 32 55 5 55 73>; |
||
while (@a) { printf('%6d', $_) for @a = dif @a; print "\n" }</ |
while (@a) { printf('%6d', $_) for @a = dif @a; print "\n" }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> -43 11 -29 -7 10 23 -50 50 18 |
<pre> -43 11 -29 -7 10 23 -50 50 18 |
||
Line 2,178: | Line 2,178: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">fwd_diff_n</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">fwd_diff_n</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">)</span> |
||
Line 2,196: | Line 2,196: | ||
<span style="color: #0000FF;">?</span><span style="color: #000000;">fwd_diff_n</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
<span style="color: #0000FF;">?</span><span style="color: #000000;">fwd_diff_n</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,212: | Line 2,212: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
function forwardDiff($anArray, $times = 1) { |
function forwardDiff($anArray, $times = 1) { |
||
Line 2,254: | Line 2,254: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">go => |
||
L = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73], |
L = [90, 47, 58, 29, 22, 32, 55, 5, 55, 73], |
||
foreach(I in 1..L.length-1) |
foreach(I in 1..L.length-1) |
||
Line 2,287: | Line 2,287: | ||
Diffs1 := Diffs1 ++ [Diff] |
Diffs1 := Diffs1 ++ [Diff] |
||
end, |
end, |
||
Diffs = Diffs1.</ |
Diffs = Diffs1.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,303: | Line 2,303: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de fdiff (Lst) |
||
(mapcar - (cdr Lst) Lst) ) |
(mapcar - (cdr Lst) Lst) ) |
||
(for (L (90 47 58 29 22 32 55 5 55 73) L (fdiff L)) |
(for (L (90 47 58 29 22 32 55 5 55 73) L (fdiff L)) |
||
(println L) )</ |
(println L) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(90 47 58 29 22 32 55 5 55 73) |
<pre>(90 47 58 29 22 32 55 5 55 73) |
||
Line 2,321: | Line 2,321: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
/* Forward differences. */ /* 23 April 2010 */ |
/* Forward differences. */ /* 23 April 2010 */ |
||
differences: procedure options (main); |
differences: procedure options (main); |
||
Line 2,343: | Line 2,343: | ||
end differences; |
end differences; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To add a fraction to some fraction things: |
||
Allocate memory for a fraction thing. |
Allocate memory for a fraction thing. |
||
Put the fraction into the fraction thing's fraction. |
Put the fraction into the fraction thing's fraction. |
||
Line 2,400: | Line 2,400: | ||
If the fraction thing's next is not nil, write ", " 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. |
Put the fraction thing's next into the fraction thing. |
||
Repeat.</ |
Repeat.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,410: | Line 2,410: | ||
=={{header|Pop11}}== |
=={{header|Pop11}}== |
||
< |
<syntaxhighlight lang="pop11">define forward_difference(l); |
||
lvars res = [], prev, el; |
lvars res = [], prev, el; |
||
if l = [] then |
if l = [] then |
||
Line 2,429: | Line 2,429: | ||
endfor; |
endfor; |
||
res; |
res; |
||
enddefine;</ |
enddefine;</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<math>\Delta^n [f](x)= \sum_{k=0}^n \left ({\prod_{i=1}^k \frac{n-k+i}{i}}\right ) (-1)^{n-k} f(x+k)</math> |
<math>\Delta^n [f](x)= \sum_{k=0}^n \left ({\prod_{i=1}^k \frac{n-k+i}{i}}\right ) (-1)^{n-k} f(x+k)</math> |
||
< |
<syntaxhighlight lang="powershell">function Forward-Difference( [UInt64] $n, [Array] $f ) |
||
{ |
{ |
||
$flen = $f.length |
$flen = $f.length |
||
Line 2,454: | Line 2,454: | ||
} |
} |
||
Forward-Difference 2 1,2,4,5</ |
Forward-Difference 2 1,2,4,5</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure forward_difference(List a()) |
||
If ListSize(a()) <= 1 |
If ListSize(a()) <= 1 |
||
ClearList(a()): ProcedureReturn |
ClearList(a()): ProcedureReturn |
||
Line 2,511: | Line 2,511: | ||
EndIf |
EndIf |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>1 [43,-11,29,7,-10,-23,50,-50,-18] |
<pre>1 [43,-11,29,7,-10,-23,50,-50,-18] |
||
Line 2,526: | Line 2,526: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Python 2=== |
===Python 2=== |
||
< |
<syntaxhighlight lang="python">>>> dif = lambda s: [x-s[i] for i,x in enumerate(s[1:])] |
||
>>> # or, dif = lambda s: [x-y for x,y in zip(s[1:],s)] |
>>> # or, dif = lambda s: [x-y for x,y in zip(s[1:],s)] |
||
>>> difn = lambda s, n: difn(dif(s), n-1) if n else s |
>>> difn = lambda s, n: difn(dif(s), n-1) if n else s |
||
Line 2,549: | Line 2,549: | ||
[-442, 575, -1329], |
[-442, 575, -1329], |
||
[1017, -1904], |
[1017, -1904], |
||
[-2921]]</ |
[-2921]]</syntaxhighlight> |
||
===Python 3=== |
===Python 3=== |
||
Defining functions for first order and nth order forward difference: |
Defining functions for first order and nth order forward difference: |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Forward difference''' |
||
Line 2,646: | Line 2,646: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>9th order forward difference of: |
<pre>9th order forward difference of: |
||
Line 2,667: | Line 2,667: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ times |
||
[ [] swap behead |
[ [] swap behead |
||
swap witheach |
swap witheach |
||
Line 2,679: | Line 2,679: | ||
dup echo say ": " |
dup echo say ": " |
||
f-diff echo cr ] |
f-diff echo cr ] |
||
drop</ |
drop</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,696: | Line 2,696: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">forwarddif <- function(a, n) { |
||
if ( n == 1 ) |
if ( n == 1 ) |
||
a[2:length(a)] - a[1:length(a)-1] |
a[2:length(a)] - a[1:length(a)-1] |
||
Line 2,716: | Line 2,716: | ||
print(forwarddif(v, 9)) |
print(forwarddif(v, 9)) |
||
print(fdiff(v, 9))</ |
print(fdiff(v, 9))</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
Line 2,732: | Line 2,732: | ||
(nth-forward-difference 9 '(90 47 58 29 22 32 55 5 55 73)) |
(nth-forward-difference 9 '(90 47 58 29 22 32 55 5 55 73)) |
||
;; -> '(-2921) |
;; -> '(-2921) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,740: | Line 2,740: | ||
The Z- operator is a zip metaoperator with a minus to subtract the two lists pairwise. |
The Z- operator is a zip metaoperator with a minus to subtract the two lists pairwise. |
||
It's almost a shame to define difn, since the series and subscript are hardly longer than the call itself would be, and arguably more self-documenting than the name of the function would be. |
It's almost a shame to define difn, since the series and subscript are hardly longer than the call itself would be, and arguably more self-documenting than the name of the function would be. |
||
<lang |
<syntaxhighlight lang="raku" line>sub dif(@array [$, *@tail]) { @tail Z- @array } |
||
sub difn($array, $n) { ($array, &dif ... *)[$n] }</ |
sub difn($array, $n) { ($array, &dif ... *)[$n] }</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,748: | Line 2,748: | ||
This version allows a specification of the list of numbers and/or which ''order'' to process. |
This version allows a specification of the list of numbers and/or which ''order'' to process. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program computes the forward difference of a list of numbers. */ |
||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/ |
numeric digits 100 /*ensure enough accuracy (decimal digs)*/ |
||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */ |
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */ |
||
Line 2,770: | Line 2,770: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</ |
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</syntaxhighlight> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 2,805: | Line 2,805: | ||
===with error checking=== |
===with error checking=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program computes the forward difference of a list of numbers. */ |
||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/ |
numeric digits 100 /*ensure enough accuracy (decimal digs)*/ |
||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */ |
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */ |
||
Line 2,833: | Line 2,833: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
ser: say; say '***error***'; say arg(1); say; exit 13 |
ser: say; say '***error***'; say arg(1); say; exit 13 |
||
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</ |
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</syntaxhighlight> |
||
'''output''' is the same as the REXX entry above. |
'''output''' is the same as the REXX entry above. |
||
===with output alignment=== |
===with output alignment=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program computes the forward difference of a list of numbers. */ |
||
numeric digits 100 /*ensure enough accuracy (decimal digs)*/ |
numeric digits 100 /*ensure enough accuracy (decimal digs)*/ |
||
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */ |
parse arg e ',' N /*get a list: ε1 ε2 ε3 ε4 ··· , order */ |
||
Line 2,869: | Line 2,869: | ||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
ser: say; say '***error***'; say arg(1); say; exit 13 |
ser: say; say '***error***'; say arg(1); say; exit 13 |
||
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</ |
th: procedure; x=abs(arg(1)); return word('th st nd rd',1+x//10*(x//100%10\==1)*(x//10<4))</syntaxhighlight> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 2,888: | Line 2,888: | ||
===Version 2=== |
===Version 2=== |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* Forward differences |
* Forward differences |
||
* 18.08.2012 Walter Pachl derived from PL/I |
* 18.08.2012 Walter Pachl derived from PL/I |
||
Line 2,922: | Line 2,922: | ||
End |
End |
||
End |
End |
||
Return</ |
Return</syntaxhighlight> |
||
{{out}} for Java's input |
{{out}} for Java's input |
||
<pre> |
<pre> |
||
Line 2,941: | Line 2,941: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Forward difference |
# Project : Forward difference |
||
Line 2,967: | Line 2,967: | ||
see svect |
see svect |
||
see "}" + nl |
see "}" + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 2,988: | Line 2,988: | ||
{{works with|Ruby|1.8.7}} |
{{works with|Ruby|1.8.7}} |
||
< |
<syntaxhighlight lang="ruby">def dif(s) |
||
s.each_cons(2).collect { |x, y| y - x } |
s.each_cons(2).collect { |x, y| y - x } |
||
end |
end |
||
Line 2,994: | Line 2,994: | ||
def difn(s, n) |
def difn(s, n) |
||
n.times.inject(s) { |s, | dif(s) } |
n.times.inject(s) { |s, | dif(s) } |
||
end</ |
end</syntaxhighlight> |
||
{{out|Example usage}} |
{{out|Example usage}} |
||
< |
<syntaxhighlight lang="ruby">p dif([1, 23, 45, 678]) # => [22, 22, 633] |
||
p difn([1, 23, 45, 678], 2) # => [0, 611]</ |
p difn([1, 23, 45, 678], 2) # => [0, 611]</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def fdiff(xs: List[Int]) = (xs.tail, xs).zipped.map(_ - _) |
||
def fdiffn(i: Int, xs: List[Int]): List[Int] = if (i == 1) fdiff(xs) else fdiffn(i - 1, fdiff(xs))</ |
def fdiffn(i: Int, xs: List[Int]): List[Int] = if (i == 1) fdiff(xs) else fdiffn(i - 1, fdiff(xs))</syntaxhighlight> |
||
{{out|Example}} |
{{out|Example}} |
||
< |
<syntaxhighlight lang="scala">val l=List(90,47,58,29,22,32,55,5,55,73) |
||
(1 to 9)foreach(x=>println(fdiffn(x,l)))</ |
(1 to 9)foreach(x=>println(fdiffn(x,l)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,022: | Line 3,022: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define (forward-diff lst) |
||
(if (or (null? lst) (null? (cdr lst))) |
(if (or (null? lst) (null? (cdr lst))) |
||
'() |
'() |
||
Line 3,032: | Line 3,032: | ||
xs |
xs |
||
(nth-forward-diff (- n 1) |
(nth-forward-diff (- n 1) |
||
(forward-diff xs))))</ |
(forward-diff xs))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,041: | Line 3,041: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func array integer: forwardDifference (in array integer: data) is func |
const func array integer: forwardDifference (in array integer: data) is func |
||
Line 3,073: | Line 3,073: | ||
data := forwardDifference(data); |
data := forwardDifference(data); |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,091: | Line 3,091: | ||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
Solution that keeps track of intermediate values: |
Solution that keeps track of intermediate values: |
||
<syntaxhighlight lang="sequencel"> |
|||
<lang sequenceL> |
|||
forwardDifference(x(1), n) := forwardDifferenceHelper(x, n, [x]); |
forwardDifference(x(1), n) := forwardDifferenceHelper(x, n, [x]); |
||
Line 3,100: | Line 3,100: | ||
result when n = 0 or size(x) = 1 else |
result when n = 0 or size(x) = 1 else |
||
forwardDifferenceHelper(difference, n - 1, result ++ [difference]); |
forwardDifferenceHelper(difference, n - 1, result ++ [difference]); |
||
</syntaxhighlight> |
|||
</lang> |
|||
If no intermediate values are needed, the following is sufficient: |
If no intermediate values are needed, the following is sufficient: |
||
<syntaxhighlight lang="sequencel"> |
|||
<lang sequenceL> |
|||
forwardDifference(x(1),n) := |
forwardDifference(x(1),n) := |
||
x when n = 0 or size(x) = 1 else |
x when n = 0 or size(x) = 1 else |
||
forwardDifference(tail(x) - x[1 ... size(x) - 1], n - 1); |
forwardDifference(tail(x) - x[1 ... size(x) - 1], n - 1); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func dif(arr) { |
||
gather { |
gather { |
||
for i (0 ..^ arr.end) { |
for i (0 ..^ arr.end) { |
||
Line 3,123: | Line 3,123: | ||
say dif([1, 23, 45, 678]) # => [22, 22, 633] |
say dif([1, 23, 45, 678]) # => [22, 22, 633] |
||
say difn(2, [1, 23, 45, 678]) # => [0, 611]</ |
say difn(2, [1, 23, 45, 678]) # => [0, 611]</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang="slate">s@(Sequence traits) forwardDifference |
||
[ |
[ |
||
s allButFirst with: s allButLast collect: #- `er |
s allButFirst with: s allButLast collect: #- `er |
||
Line 3,142: | Line 3,142: | ||
[ |
[ |
||
(0 below: n) inject: s into: [| :seq :_ | seq forwardDifference] |
(0 below: n) inject: s into: [| :seq :_ | seq forwardDifference] |
||
].</ |
].</syntaxhighlight> |
||
{{out|Usage}} |
{{out|Usage}} |
||
< |
<syntaxhighlight lang="slate">#data := ##(90 47 58 29 22 32 55 5 55 73). |
||
data keysDo: [| :index | inform: (data forwardDifference: index) printString].</ |
data keysDo: [| :index | inform: (data forwardDifference: index) printString].</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
{{works with|GNU Smalltalk}} |
{{works with|GNU Smalltalk}} |
||
< |
<syntaxhighlight lang="smalltalk">Array extend [ |
||
difference [ |
difference [ |
||
^self allButFirst with: self allButLast collect: [ :a :b | a - b ] |
^self allButFirst with: self allButLast collect: [ :a :b | a - b ] |
||
Line 3,163: | Line 3,163: | ||
s := #(90 47 58 29 22 32 55 5 55 73) |
s := #(90 47 58 29 22 32 55 5 55 73) |
||
1 to: s size - 1 do: [ :i | |
1 to: s size - 1 do: [ :i | |
||
(s nthOrderDifference: i) printNl ]</ |
(s nthOrderDifference: i) printNl ]</syntaxhighlight> |
||
=={{header|SQL}}== |
=={{header|SQL}}== |
||
Line 3,169: | Line 3,169: | ||
{{works with|SQLite|3.13.0}} |
{{works with|SQLite|3.13.0}} |
||
< |
<syntaxhighlight lang="sql">WITH RECURSIVE |
||
T0 (N, ITEM, LIST, NEW_LIST) AS |
T0 (N, ITEM, LIST, NEW_LIST) AS |
||
( |
( |
||
Line 3,205: | Line 3,205: | ||
WHERE NEW_LIST IS NULL |
WHERE NEW_LIST IS NULL |
||
AND LIST <> '' |
AND LIST <> '' |
||
ORDER BY N;</ |
ORDER BY N;</syntaxhighlight> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">fun forward_difference xs = ListPair.map op- (tl xs, xs) |
||
fun nth_forward_difference n xs = |
fun nth_forward_difference n xs = |
||
Line 3,215: | Line 3,215: | ||
xs |
xs |
||
else |
else |
||
nth_forward_difference (n-1) (forward_difference xs)</ |
nth_forward_difference (n-1) (forward_difference xs)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,226: | Line 3,226: | ||
It's possible to implement differences using row indices. For instance, first forward differences of a variable x can be defined by: |
It's possible to implement differences using row indices. For instance, first forward differences of a variable x can be defined by: |
||
< |
<syntaxhighlight lang="stata">gen y=x[_n+1]-x[_n]</syntaxhighlight> |
||
However, it's much more natural to use [http://www.stata.com/help.cgi?tsvarlist time-series varlists]. In order to use them, it's necessary to first set a ''time'' variable, which may be simply an index variable. |
However, it's much more natural to use [http://www.stata.com/help.cgi?tsvarlist time-series varlists]. In order to use them, it's necessary to first set a ''time'' variable, which may be simply an index variable. |
||
< |
<syntaxhighlight lang="stata">* First create a dataset |
||
clear all |
clear all |
||
set obs 100 |
set obs 100 |
||
Line 3,239: | Line 3,239: | ||
* Differences |
* Differences |
||
display "Difference order?" _request(k) |
display "Difference order?" _request(k) |
||
gen y=D${k}F${k}.x</ |
gen y=D${k}F${k}.x</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func forwardsDifference<T: SignedNumeric>(of arr: [T]) -> [T] { |
||
return zip(arr.dropFirst(), arr).map({ $0.0 - $0.1 }) |
return zip(arr.dropFirst(), arr).map({ $0.0 - $0.1 }) |
||
} |
} |
||
Line 3,262: | Line 3,262: | ||
for diff in (0...9).map({ nthForwardsDifference(of: [90, 47, 58, 29, 22, 32, 55, 5, 55, 73], n: $0) }) { |
for diff in (0...9).map({ nthForwardsDifference(of: [90, 47, 58, 29, 22, 32, 55, 5, 55, 73], n: $0) }) { |
||
print(diff) |
print(diff) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,278: | Line 3,278: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc do_fwd_diff {list} { |
||
set previous [lindex $list 0] |
set previous [lindex $list 0] |
||
set new [list] |
set new [list] |
||
Line 3,300: | Line 3,300: | ||
for {set order 0} {$order <= 10} {incr order} { |
for {set order 0} {$order <= 10} {incr order} { |
||
puts [format "%d\t%s" $order [fwd_diff $a $order]] |
puts [format "%d\t%s" $order [fwd_diff $a $order]] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 90.5 47 58 29 22 32 55 5 55 73.5 |
<pre>0 90.5 47 58 29 22 32 55 5 55 73.5 |
||
Line 3,317: | Line 3,317: | ||
This function doesn't need to be defined because it's in a library already, |
This function doesn't need to be defined because it's in a library already, |
||
but it could be defined like this: |
but it could be defined like this: |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#import nat |
#import nat |
||
#import flo |
#import flo |
||
nth_diff "n" = rep"n" minus*typ</ |
nth_diff "n" = rep"n" minus*typ</syntaxhighlight> |
||
test program: |
test program: |
||
< |
<syntaxhighlight lang="ursala">test_data = <90.,47.,58.,29.,22.,32.,55.,5.,55.,73.> |
||
#show+ |
#show+ |
||
Line 3,331: | Line 3,331: | ||
printf/*=*' %0.0f' < |
printf/*=*' %0.0f' < |
||
nth_diff6 test_data, |
nth_diff6 test_data, |
||
nth_diff7 test_data></ |
nth_diff7 test_data></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 291 -151 424 -905 |
<pre> 291 -151 424 -905 |
||
Line 3,338: | Line 3,338: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{works with|Visual Basic .NET|2008+}} |
{{works with|Visual Basic .NET|2008+}} |
||
< |
<syntaxhighlight lang="vbnet">Module ForwardDifference |
||
Sub Main() |
Sub Main() |
||
Line 3,359: | Line 3,359: | ||
End Function |
End Function |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,375: | Line 3,375: | ||
=={{header|Visual FoxPro}}== |
=={{header|Visual FoxPro}}== |
||
< |
<syntaxhighlight lang="vfp"> |
||
#DEFINE CTAB CHR(9) |
#DEFINE CTAB CHR(9) |
||
LOCAL lcList As String, i As Integer, n As Integer |
LOCAL lcList As String, i As Integer, n As Integer |
||
Line 3,413: | Line 3,413: | ||
? lcTxt |
? lcTxt |
||
ENDPROC |
ENDPROC |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,430: | Line 3,430: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var forwardDiff = Fn.new { |a, order| |
var forwardDiff = Fn.new { |a, order| |
||
Line 3,447: | Line 3,447: | ||
} |
} |
||
forwardDiff.call([90, 47, 58, 29, 22, 32, 55, 5, 55, 73], 9)</ |
forwardDiff.call([90, 47, 58, 29, 22, 32, 55, 5, 55, 73], 9)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,465: | Line 3,465: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Scheme}} |
{{trans|Scheme}} |
||
< |
<syntaxhighlight lang="zkl">fcn forwardDiff(lst){ |
||
if(lst.len()<2) |
if(lst.len()<2) |
||
return(T); |
return(T); |
||
Line 3,474: | Line 3,474: | ||
return(xs); |
return(xs); |
||
return(nthForwardDiff(n-1,forwardDiff(xs))) // tail recursion |
return(nthForwardDiff(n-1,forwardDiff(xs))) // tail recursion |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">nthForwardDiff(9,T(90, 47, 58, 29, 22, 32, 55, 5, 55, 73)).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,482: | Line 3,482: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
< |
<syntaxhighlight lang="zxbasic">10 DATA 9,0,1,2,4,7,4,2,1,0 |
||
20 LET p=1 |
20 LET p=1 |
||
30 READ n: DIM b(n) |
30 READ n: DIM b(n) |
||
Line 3,495: | Line 3,495: | ||
120 FOR i=1 TO n-p |
120 FOR i=1 TO n-p |
||
130 PRINT b(i);" "; |
130 PRINT b(i);" "; |
||
140 NEXT i</ |
140 NEXT i</syntaxhighlight> |