let mul = function f -> (function (x, y) -> exp (f (log (x), log (y)))) ;;
let integer = function f -> (function x -> int_of_float (f (float_of_int (x)))) ;;
let comp = function
(f1, f2, f3) -> (function x -> let (a, b) = f1 (x) in f3 (f2 (a), f2 (b))) ;;
let times = mul (function (a, b) -> a +. b) ;;
times (3., 5.) ;;
let root = integer (sqrt) ;;
root (81) ;;
let c = comp ((function x -> x / 4, x mod 4),
(function x -> x * 3),
(function q, r -> 4 * q + r)) ;;
c (9) ;;
let map_to_each = function
f -> (let rec g = function
lx -> if is_empty_list (lx)
then empty_list ()
else cons_list (f (head (lx)), g (tail (lx)))
in
g) ;;
map_to_each (function x -> 2 * x) ;;
(map_to_each (function x -> 2 * x)) ([1 ; 2 ; 3]) ;;
map_to_each (function x -> 3 * x + 1) ;;
(map_to_each (function x -> 3 * x + 1)) ([1 ; 2 ; 3]) ;;
List.map ;;
List.map (function x -> 2 * x) ;;
(List.map (function x -> 2 * x)) ([1 ; 2 ; 3]) ;;
List.map (function x -> 3 * x + 1) ;;
(List.map (function x -> 3 * x + 1)) ([1 ; 2 ; 3]) ;;
let curry = function f -> (function x -> (function y -> f (x, y))) ;;
let uncurry = function f -> (function (x, y) -> (f (x)) (y)) ;;
let plus = function x, y -> x + y ;;
let plus_curry = function x -> (function y -> x + y) ;;
plus (3, 7) ;;
curry (plus) ;;
(curry (plus)) (3) ;;
((curry (plus)) (3)) (7) ;;
plus_curry (3) ;;
(plus_curry (3)) (7) ;;
uncurry (plus_curry) ;;
(uncurry (plus_curry)) (3, 7) ;;
let exchange = function f -> (function x -> (function y -> (f (y)) (x))) ;;
let rec accum = function
(op, neutral) -> (let rec
f = function
lx -> if is_empty_list (lx)
then neutral
else op (head (lx), f (tail (lx)))
in
f) ;;
let accum_1 = function
f -> exchange (function neutral -> accum (uncurry (f), neutral)) ;;
accum_1 (function x -> (function y -> x + y)) ;;
(accum_1 (function x -> (function y -> x + y))) ([1 ; 2 ; 3]) ;;
((accum_1 (function x -> (function y -> x + y))) ([1 ; 2 ; 3])) (7) ;;
((accum_1 (function x -> (function y -> x - y))) ([1 ; 2 ; 3])) (7) ;;
List.fold_right ;;
((List.fold_right (function x -> (function y -> x + y))) ([1 ; 2 ; 3])) (7) ;;
((List.fold_right (function x -> (function y -> x - y))) ([1 ; 2 ; 3])) (7) ;;
let rec fp = function f -> (function x -> if x = f (x) then x else (fp (f)) (f (x))) ;;
fp (function x -> (x +. 2.) /. (x +. 1.)) ;;
(fp (function x -> (x +. 2.) /. (x +. 1.))) (0.) ;;
|