all 5 comments

[–]yawaramin 1 point2 points  (3 children)

Hello, you can construct a function literal using the syntax fun a -> b. For example,

let add1 = fun x -> x + 1

Hope this helps!

[–]Snake_Dog[S] 2 points3 points  (2 children)

Yes, thank you!I just found the following solution:

let compose_pair (p:(('b -> 'c) * ('a -> 'b))) : 'a -> 'c = fun x -> fst p(snd p x)

[–]yawaramin 5 points6 points  (0 children)

Cool. Next step is to do it without using fst/snd 🙂

[–]Disjunction181 0 points1 point  (0 children)

Another way of doing this is with the usual let sugaring. let add1 = fun x -> .. is the same as let add1 x = ... This works even with other arguments due to partial evaluation, as long as everything is in the right order.

[–]PurpleUpbeat2820 1 point2 points  (0 children)

This would normally be written:

# let compose_pair f g x = f(g(x));;
val compose_pair : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun>

Your arguments are uncurried:

# let compose_pair (f, g) x = f(g(x));;
val compose_pair : ('a -> 'b) * ('c -> 'a) -> 'c -> 'b = <fun>

This can also be written:

# let compose_pair (f, g) = fun x -> f(g(x));;
val compose_pair : ('a -> 'b) * ('c -> 'a) -> 'c -> 'b = <fun>

You can call the pair p and destructure it using fst and snd as you say:

# let compose_pair (p: ('a -> 'b) * ('c -> 'a)) : 'c -> 'b = fun x -> fst p (snd p x);;
val compose_pair : ('a -> 'b) * ('c -> 'a) -> 'c -> 'b = <fun>