Apply a callback to an array: Difference between revisions

Content added Content deleted
Line 692: Line 692:
let values = Array.init 10 ((+) 1);;
let values = Array.init 10 ((+) 1);;
Array.map square values;;</lang>
Array.map square values;;</lang>

Or with lists (which are more typical in OCaml):
<lang ocaml>let values = [1;2;3;4;5;6;7;8;9;10];;
List.map square values;;</lang>

Use <tt>iter</tt> if the applied function does not return a value.

<lang ocaml>Array.iter (fun x -> Printf.printf "%d" x) [|1; 2; 3; 4; 5|];;</lang>
<lang ocaml>List.iter (fun x -> Printf.printf "%d" x) [1; 2; 3; 4; 5];;</lang>


=={{header|Octave}}==
=={{header|Octave}}==