Valery Belov
Valery Belov

Reputation: 31

What's the difference between type1 -> type2 -> type3 and type1 -> (type2 -> type3)?

Why

let f1 a b = a + b

and

let f2 a = fun b -> a + b 

both produce values with type a:int -> b:int -> int,
but

let f3 a =
   let f = 
     fun b -> a + b 
   f

and

let f4 a =
   let f b = a + b
   f

both produce values with type a:int -> (int -> int) ?
What do braces around int -> int mean? Are there situations where these two types do different things in a program?

Upvotes: 3

Views: 101

Answers (2)

SuperJumbo
SuperJumbo

Reputation: 541

Like mclark1129 says, there's no difference; you can read it as either a function of two arguments or two functions of a single argument – the brackets here denote a function because if you supply only the first argument (type1) the function will return a function that takes a type2 and returns a type3.

Currying and partial application are foundational to functional programming and underpin things like algebraic data structures where currying/partially applying multi-arity functions down to single argument functions are the norm.

Upvotes: 0

mclark1129
mclark1129

Reputation: 7592

AFAIK there is no difference, type1 -> type2 -> type3 is just syntactic sugar for type1 -> (type2 -> type3)

https://fsharpforfunandprofit.com/posts/currying/

The answer is quite simple: a function with multiple parameters is rewritten as a series of new functions, each with only one parameter. And this is done automatically by the compiler for you.

Upvotes: 3

Related Questions