Jump to content

Apply a callback to an array: Difference between revisions

Line 692:
let values = Array.init 10 ((+) 1);;
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}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.