Jump to content

Maximum difference between adjacent elements of list: Difference between revisions

m (→‎{{header|Julia}}: List may contain all real number types.)
(→‎OCaml: add)
Line 18:
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F maxDeltas(ns)
V pairs = zip(ns, ns[1..]).map((a, b) -> (abs(a - b), (a, b)))
Line 507 ⟶ 506:
idx : 12 values 2.0000000000000000E+000 9.0000000000000000E+000
idx : 15 values 1.0000000000000000E+001 3.0000000000000000E+000 </pre>
 
 
=={{header|FreeBASIC}}==
Line 590 ⟶ 588:
7 2 9
7 10 3</pre>
 
 
=={{header|jq}}==
Line 682 ⟶ 679:
<pre>The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: (1, 8) (2, 9) (10, 3)</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec fold_pairwise f m = function
| a :: (b :: _ as t) -> fold_pairwise f (f m a b) t
| _ -> m
 
let pair_to_str (a, b) =
Printf.sprintf "(%g, %g)" a b
 
let max_diffs =
let next m a b =
match abs_float (b -. a), m with
| d', (d, _) when d' > d -> d', [a, b]
| d', (d, l) when d' = d -> d', (a, b) :: l
| _ -> m
in fold_pairwise next (0., [])
 
let () =
let d, l = max_diffs [1.;8.;2.;-3.;0.;1.;1.;-2.3;0.;5.5;8.;6.;2.;9.;11.;10.;3.] in
List.rev_map pair_to_str l |> String.concat " " |> Printf.printf "%g: %s\n" d</syntaxhighlight>
{{out}}
<pre>7: (1, 8) (2, 9) (10, 3)</pre>
 
=={{header|Pascal}}==
Line 855 ⟶ 874:
 
=={{header|Raku}}==
 
<syntaxhighlight lang="raku" line>sub max-diff (*@list) {
return 0 if +@list < 2;
559

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.