let rec e1q1 n s = if n = 0 then "" else s ^ e1q1 (n-1) s (* The current digit is n mod 10. The next digits are n/10. *) let rec e1q2 n s = if n = 0 then "" else string_of_int (n mod 10) ^ s ^ (e1q2 (n/10) s) let rec e1q3 n s = if n = 0 then "" else (e1q3 (n/10) s) ^ string_of_int (n mod 10) ^ s let rec e1q4 n = if n = 0 then 0 else 1 + e1q4 (n/10) let rec e1q5 n even = if n = 0 then true else let digit = n mod 10 in (if even then digit mod 2 = 0 else digit mod 2 = 1) && e1q5 (n/10) even (* Another version. *) let e1q5 n even = let accept = if even then fun x -> x mod 2 = 0 else fun x -> x mod 2 = 1 in let rec loop n = n = 0 || (accept n && loop (n/10)) in loop n let rec e1q6 n m = if n = 0 && m = 0 then true else n mod 10 <= m mod 10 && e1q6 (n / 10) (m / 10) (* Curried functions *) let curry3 f a b c = f (a,b,c) let uncurry3 f (a,b,c) = f a b c let apply3 (f,g,h) (a,b,c) = (f a, g b, h c) let sort2 f g x = if f x < g x then (f,g) else (g,f) let comp3 f g h x = f (g (h x)) (* Pattern matching *) (* We list all the cases, using the 'whatever' pattern _, and factorizing a few patterns. *) let ext_and a b c = match (a,b,c) with | false, _, true | _, false, true -> false | false, _, false | _, false, false -> true | true, true, true -> true | true, true, false -> false let code_char = function | 'a' -> 'e' | 'e' -> 'i' | 'o' -> 'a' | 'i' -> 'y' | 'u' -> 'o' | 'y' -> 'u' | 'A' -> 'E' | 'E' -> 'I' | 'O' -> 'A' | 'I' -> 'Y' | 'U' -> 'O' | 'Y' -> 'U' | c -> c let encode s = String.map code_char s