OCaml: Currying without specific values

I have two functions f and g, and I'm trying to return f(g(x)) , but I don't know the value of x, and I'm not sure how to do this.

More specific example: if I have functions f = x + 1 and g = x * 2 and I try to return f(g(x)) , I should get a function equal to (x*2) + 1

+4
source share
1 answer

It looks like everything is fine with you, f(g(x)) should work fine. I'm not sure why you have the return keyword (this is not a keyword in ocaml). Here is the correct version,

 let compose fgx = f (gx) 

Type definition for this:

 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 have to be consistent in the definition (so the domain g must be in the range f ).

 let x_plus_x_plus_1 = compose (fun x -> x + 1) (fun x -> x * 2) 
+5
source

Source: https://habr.com/ru/post/1308317/


All Articles