落書きノート

ふと自分が気になった事を書いてます

OCamlで不定期練習

配列、リストなどなどで、やっと本格的になってきたような…。こんな感じです。

open Printf;;
let first = [0.0; 0.0; 0.0; 0.0; 1.0];;
let rec show xs i = match xs with
    [] -> ()
  | x :: xs -> printf "first[%d] = %f\n" i x;
               show xs (i + 1);;
show first 0;;



open Printf;;
let first = [5; 4; 3; 2; 1];;
let rec main i xs = match xs with
    [] -> ()
  | x :: xs -> printf "first[%d] = %d\n" i x;
               main (i + 1) xs;;
main 0 first;;




open Printf;;
let first = Array.make 5 0.0;;
          
for i = 1 to 5 do
  first.(i - 1) <- float_of_int (i) *. 1.1;
done;;

let rec main xs i =
  match xs with
    [] -> ()
  | x :: xs -> printf "first[%d] = %.1f\n" i x;
               main xs (i + 1);;

main (Array.to_list first) 0;;