let int_of_bool =
  function
    false -> 0
  | true -> 1

(* Generates the 2^n sub lists, of same order than the input list of length n.
   Nice property : exactly one element of a list changes at a time.

   # sub_lists ['~'; '/'; '&'];;
   - : char list list =
   [['~']; ['~'; '&']; ['~'; '/'; '&']; ['~'; '/']; ['/']; ['/'; '&']; ['&'];
    []]
 *)
let rec sub_lists =
  function
    [] -> [[]]
  | h::t ->
    let s = sub_lists t in
      List.fold_left (fun a l -> (h::l)::a) s s

(* Asserts arg > 0 *) 
let rec list_of_len =
  function
    1 -> [1]
  | n -> n::(list_of_len (n-1))

let gray n =
  let l = list_of_len n in
    List.iter
      (fun s ->
        List.iter
          (fun i -> Printf.printf "%i" (int_of_bool (List.mem i s)))
          l;
        Printf.printf "\n")
      (sub_lists l)

let () = gray 4

This document was generated using caml2html