I’m struggling with the F# type signature notation. For example let’s say you have a Fold function:
let rec Fold combine acc l =
...
that may have this type signature:
('a -> 'b -> 'a) -> 'a -> list<'b> -> 'a
which I would read as
a function that has three arguments:
- a function that takes an ‘a, a ‘b and returns an a’
- an ‘a
- a list of ‘b
and returns an ‘a.
But then it would make more sense for my cavemen brain to express it as
('a, 'b -> 'a), 'a, list<'b> -> 'a
I’m sure there is a semantic reason why parameters are separated with an arrow exactly the same way as the function return type, but somehow I’m missing it and didn’t found a clear explanation in books/articles so far. Every time I see a type signature I have to stop quite a bit of time to understand it. I feel like I’m just missing that little piece of the puzzle that makes the “decryption” obvious.
Can someone please enlighten me?
You’re reading of the first function is correct. For instant deciphering, type signatures are expressed like this:
Generally, arrow notation indicates a function is curry-able.
Because the function is curried, you can technically write it like this:
If you think about it, the
add4signature is equivalent to this:I believe we use arrow notation because it resembles the structure of the function when we explicitly curry arguments as shown above.