Passing F #

I always thought that F # has two different ways to pass arguments, curry style and tuple style. Is this really right?

Isn't that just one style, curry style and arguments can be either simple values ​​or tuples.

eg.

someFunc (a,b) = 

isn't this a one-argument function of curry style that is a tuple? Thus, letting me pass tuples to this function using the pipleline operator? (where the elements of the tuple are named)

 (1,2) |> someFunc 

Is it correct?

+6
source share
2 answers

This will work very well - the difference is what you have

 let f (a,b) = ... let f2 ab = ... 

then you can easily create a partially applied f2, but for f it doesn't work as well - you need to do

 let partial = fun t -> f (1,t) let partial2 = f2 1 
+6
source

Yes, all F # features are "curry style." When you have a type definition:

 let someFunc (a,b) = a + b 

You have a function that takes one argument, a tuple that decomposes into a pattern (yes, pattern matching is available in surprisingly sweet places like this). This is equivalent to the following definition, which moves the pattern matching to the function body:

 let someFunc t = match t with | a, b -> a + b 

Which is also equivalent

 let someFunc = function | a, b -> a + b 

The first version with matching patterns in the argument itself is clearly preferable in this example to simple noun bindings.

Note that F # methods , however, are a "tuple" (this is one of those places where F # glues with a standard .NET object-oriented function).

+4
source

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


All Articles