Brackets replace function

When I place brackets in a function definition, the types of functions change.

I have two functions: pad1 (without parentheses) and pad2 (with parentheses). The types are the same, but the function signature is different. Why is this type different?

let addition1 ab = a + b //val addition1 : a:int -> b:int -> int let addition2(a, b) = a + b //val addition2 : a:int * b:int -> int 
+5
source share
1 answer

The types are the same, but the function signature is different. Why is the type different?

The types do not really match.

When you write:

 let addition1 ab = a + b 

You create a function that is different from

 let addition2 (a, b) = a + b 

In the second case, the brackets and comma create a tuple , which means that your function takes a single parameter, which is a tuple (typed as int * int ) and returns int .

The first case creates a function that can be specified. A typical signature int -> int -> int means that it creates a function that takes int, and then returns a function that accepts int and returns int. This allows you to use a partial application:

 let partially_applied_addition1 = addition1 3 

See functions in white papers and Currying from fsharpforfunandprofit for details .

Currying permission is much more common in F # code. In the general case, the use of tuples as a parameter is mainly performed for scenarios of interaction with libraries of the base class or when planning the API to be used with C # or other languages. The possibility of partial application thus allows you to work as a pipeline:

 let answer = getSomeIntegerValue () |> addition1 12 // Add 12 to result 

The above form will not compile with the above, since it cannot work with a partial application.

+9
source

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


All Articles