Reputation: 2619
I have two functions f and g and I am trying to return f(g(x))
but I do not know the value of x and I am not really sure how to go about this.
A more concrete example: if I have functions f = x + 1
and g = x * 2
and I am trying to return f(g(x))
I should get a function equal to (x*2) + 1
Upvotes: 4
Views: 414
Reputation: 47934
It looks like you have it right, f(g(x))
should work fine. I'm not sure why you have a return
keyword there (it's not a keyword in ocaml). Here is a correct version,
let compose f g x = f (g x)
The type definition for this is,
val compose : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c = <fun>
Each, 'a,'b,'c are abstract types; we don't care what they are, they just need to be consistent in the definition (so, the domain of g
must be in the range of f
).
let x_plus_x_plus_1 = compose (fun x -> x + 1) (fun x -> x * 2)
Upvotes: 5