落書きノート

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

OCamlで不定期練習

Random.self_init ();;
print_string "要素数 : ";;
let n = read_int ();;
let array = Array.make n 0;;

for i = 0 to n - 1 do
  array.(i) <- Random.int 10 + 1;
done;;

let rec show index = if index = 0 then
                       ()
                     else
                       (
                         for i = 0 to n - 1 do
                           if array.(i) = index then
                             (
                               print_string "* ";
                               array.(i) <- array.(i) - 1;
                             )
                           else
                             print_string "  ";
                         done;
                         print_newline ();
                         show (index - 1);
                       );;

let rec dashed index = if index = 0 then
                         (
                           print_newline ();
                         )
                       else
                         (
                           print_string "--";
                           dashed (index - 1);
                         );;

let rec indexed index = if index = n then
                          (
                            print_newline ();
                          )
                        else
                          (
                            print_int (index mod 10);
                            print_string " ";
                            indexed (index + 1);
                          );;

show 10;;
dashed n;;
indexed 0;;