let x = 12;; let y = 13;; let x = 25;; x;; Nil;; [];; 1::[];; 2::1::[];; [[1;2];[4;5]];; succ;; pred;; [succ;pred];; [1;'1'];; [1;2;3;4];; let hd xs = match xs with | [] -> failwith "hd: empty list" | x::xs -> x;; hd [1;2;3;4];; hd [1];; hd [];; let hd xs = match xs with | x::xs -> x;; hd [];; let hd = function | x::xs -> x;; let hd = function | x::_ -> x;; let hd (x::_) = x;; let hd [];; hd [];; hd [1;2;3];; let fst_or_snd xs = match xs | [x] -> x | _::y::_ -> y | _ -> failwith "empty list";; let rec replicate n x = if n < 1 then [] else x::replicate (n-1) x;; replicate 2 'c';; #trace replicate;; replicate 2 'c';; let rec range m n = if m >= n then [] else m::range (m+1) n;; (>=);; (+);; range 1 3;; range 1 100;; range 1 1000;; range 1 100000;; range 1 1000000;; let rec range m n = if m >= n then [] else m::range m n;; range 1 3;; let rec sum = function | [] -> 0 | x::xs -> x + sum xs;; let rec prod = function | [] -> 1 | x::xs -> x * prod xs;; prod [1;2;3;4;5];; let rec foldr f b xs = match xs with | [] -> b | x::xs -> f x (foldr f b xs);; let sum xs = foldr (+) 0 xs;; sum [1;2;3;4;5];; let prod xs = foldr ( * ) 1 xs;; prod [1;2;3;4;5];; let (@) xs ys = match xs with [] -> ys | x::xs -> x::(xs@ys);; let rec foldr = function [] -> b | x::xs -> f x (foldr f b xs);; let rec foldr f b = function [] -> b | x::xs -> f x (foldr f b xs);;