Forward difference: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 401:
}
Return list
}</lang>
 
=={{header|AWK}}==
Line 878:
(loop repeat n do (map-into list #'- (rest list) list))
(subseq list 0 (- (length list) n)))</lang>
 
 
=={{header|D}}==
Line 990 ⟶ 989:
flutter: []
</pre>
 
 
=={{header|E}}==
Line 1,104 ⟶ 1,102:
ok
</pre>
 
=={{header|F_Sharp|F#}}==
Straightforward recursive solution
<lang fsharp>let rec ForwardDifference input n =
match n with
| _ when input = [] || n < 0 -> [] // If there's no more input, just return an empty list
| 0 -> input // If n is zero, we're done - return the input
| _ -> ForwardDifference // otherwise, recursively difference..
(input.Tail // All but the first element
|> Seq.zip input // tupled with itself
|> 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</lang>
 
=={{header|Factor}}==
Line 1,135 ⟶ 1,145:
 
test</lang>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
Line 1,183 ⟶ 1,194:
-2921
</pre>
 
=={{header|F_Sharp|F#}}==
Straightforward recursive solution
<lang fsharp>let rec ForwardDifference input n =
match n with
| _ when input = [] || n < 0 -> [] // If there's no more input, just return an empty list
| 0 -> input // If n is zero, we're done - return the input
| _ -> ForwardDifference // otherwise, recursively difference..
(input.Tail // All but the first element
|> Seq.zip input // tupled with itself
|> 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</lang>
 
=={{header|Go}}==
Line 1,901 ⟶ 1,900:
1017 -1904
-2921</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo Star|2010-07}}
<p>Here we use signature matching to bind both an entire array and a version missing the head.
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.
<lang perl6>sub dif(@array [$, *@tail]) { @tail Z- @array }
sub difn($array, $n) { ($array, &dif ... *)[$n] }</lang>
 
=={{header|Phix}}==
Line 2,207 ⟶ 2,198:
;; -> '(-2921)
</lang>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
{{works with|Rakudo Star|2010-07}}
<p>Here we use signature matching to bind both an entire array and a version missing the head.
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.
<lang perl6>sub dif(@array [$, *@tail]) { @tail Z- @array }
sub difn($array, $n) { ($array, &dif ... *)[$n] }</lang>
 
=={{header|REXX}}==
Line 2,892:
Difference 9: -2921
</pre>
 
=={{header|zkl}}==
{{trans|Scheme}}
10,327

edits