(* let rec binary n = if (n = 0) then [] else (n mod 2)::(binary (n / 2));; *) let binary n = unfoldr (fun x -> x = 0) (fun x -> x mod 2) (fun x -> x / 2) n (* let rec decimal b = match b with [] -> 0 | x::xs -> x + 2 * (decimal xs) *) let decimal b = foldr (fun x y -> x + 2 * y) 0 b let rec pow m n = if n = 0 then 1 else m * (pow m (n - 1)) (* calculate m^{bn} *) let fastpow m bn = foldr (fun x y -> y * y * if x = 1 then m else 1) 1 bn