Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define function 'a -> 'b

Tags:

ocaml

i have a task to define two functions, one with type:

 ('a-> 'b) -> ('c -> 'a) -> 'c -> 'b

and second one with just

'a -> 'b

while the first function is composition and i've done that this way:

let compose f g arg = f(g(arg))

i'm stuck on the second one. could anyone lead me to correct answer? i made this one:

let a x = List.hd []

but im pretty sure there is much simplier example.

like image 368
blahblah Avatar asked Jul 09 '26 05:07

blahblah


1 Answers

The first function is not currying, it's function composition. Currying has the type ('a * 'b -> 'c) -> 'a -> 'b -> 'c.

Your solution for 'a -> 'b is pretty good. Note that List.hd [] raises an exception. So another implementation is:

let f x = raise Not_found

The thing to realize is that no "nice" function can possibly have the given type. Where would it get an actual value of type 'b to return? It would have to be a value that can have any type at all.

like image 51
Jeffrey Scofield Avatar answered Jul 11 '26 20:07

Jeffrey Scofield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!