let rec f x = if x = 0 then 0 else f x;; let rec a = a;; f 0;; f 1;; List.hd (f 0 :: f 1 :: []);; let rec from n = n :: from (n + 1);; List.hd (from 0);; type 'a llist = Nil | Cons of 'a * (unit -> 'a llist);; let hd = function (Cons (x, _)) -> x | Nil -> failwith "hd";; let tl = function (Cons (_, t)) -> t () | Nil -> failwith "tl";; let rec from n = Cons (n, fun () -> from (n + 1));; from 0;; hd (from 0);; tl (from 0);; let rec to_list n xs = if n < 1 then [] else match xs with Nil -> [] | Cons (x, t) -> x :: (to_list (n - 1) (t ()));; to_list 3 (from 0);; to_list 1000 (from 0);; type 'a cell = Nil | Cons of 'a * 'a llist and 'a llist = unit -> 'a cell;; let rec tl xs = match xs () with Cons (_, t) -> t | Nil -> failwith "tl";; let rec zipwith f xs ys = match (xs (), ys ()) with (Cons (x, xs), Cons (y, ys)) -> fun () -> Cons (f x y, zipwith f xs ys) | _ -> fun () -> Nil;; let rec fibs = fun () -> Cons (0, fun () -> Cons (1, zipwith (+) fibs (tl fibs)));; let rec to_list n xs = if n < 1 then [] else match xs () with Nil -> [] | Cons (x, t) -> x :: to_list (n - 1) t;; to_list 0 fibs;; to_list 1 fibs;; to_list 2 fibs;; open UnitList;; to_list 10 fibs;; to_list 20 fibs;; to_list 32 fibs;; let e = lazy (print_string "bla");; lazy.force e;; Lazy.force e;; open LazyList;; to_list 30 fibs;; to_list 60 fibs;; primes;; to_list 30 primes;; to_list 1000 primes;; List.rev (to_list 1000 primes);; let x = ref 3;; !x;; x := 2;; !x;;