Function signatures

Viewed 125

I would like to create a function map3 that, given a function f and a tuple (a, b, c) applies f to every member of the tuple. The expected signature of the function should be:

('a -> 'b) -> 'a * 'a * 'a -> 'b * 'b * 'b .

I tried several approaches:

> let map3 = fun x -> let f = fun (a, b, c) -> (a, b, c) in f x;;
val map3 : 'a * 'b * 'c -> 'a * 'b * 'c

> let map3 = fun x y -> x (let f = fun (a, b, c) -> (a, b, c) in f y);;
val map3 : x:('a * 'b * 'c -> 'd) -> 'a * 'b * 'c -> 'd

> let map3 = fun (x, y, z) -> let f = fun (a, b, c) -> (a, b, c) in f (x, y, z);; 
val map3 : x:'a * y:'b * z:'c -> 'a * 'b * 'c

I strongly suspect that I did not get how function signatures actually work. According to what I get, map3 should have one input and one output, and f should take as input a triple and return a triple. But it is clearly wrong on something. What am I missing, in my attempts to implement it?

1 Answers
let map3 f (x, y, z) = (f x, f y, f z)

you can also write it like

let map3 f = fun (x, y, z) -> (f x, f y, f z)

or

let map3 = fun f (x, y, z) -> (f x, f y, f z)

But I would prefer to have the parameters on the left side. As you can see the first version is shorter to write and easier to read.

Related