
(* triangle de Pascal modulo 7 *)

module Array = struct

  let rec nth (a : int list) (i : int) : int = match a with
    | [] -> 0
    | x :: l -> if i = 0 then x else nth l (i-1)

  let get (a : int list ref) (i : int) : int = 
    nth !a i

  let rec upd (a : int list) (i : int) (v : int) : int list = match a with
    | [] -> []
    | x :: l -> if i = 0 then v :: l else x :: upd l (i-1) v

  let set (a : int list ref) (i : int) (v : int) : unit =
    a := upd !a i v

  let rec listn (n : int) (v : int) : int list = 
    if n = 0 then [] else v :: listn (n-1) v

  let create (n : int) (v : int) : int list ref =
    ref (listn n v)

end

module Pascal(X : sig val h : int end) = struct

  let r : int list ref = Array.create (X.h + 1) 0

  let rec compute_row (i : int) (j : int) : unit =
    let v = if j = 0 then 1 else (Array.get r j + Array.get r (j-1)) mod 7 
    in
    Array.set r j v;
    if j > 0 then compute_row i (j-1)

  let rec print_row (i : int) (j : int) : unit =
    if j <= i then (print_int (Array.get r j); print_row i (j+1))

  let rec triangle (i : int) : unit = 
    compute_row i i;
    print_row i 0;
    print_newline ();
    if i < X.h then triangle (i+1)

  let () = triangle 0

end

module H = struct let h : int = 13 end
module M = Pascal(H)

