7

I've always thought that F# had two different ways to pass arguments, curry style and tuple style. Is this actually correct?

Isn't it simply one style , curry style, and arguments can either be simple values or tuples.

e.g.

someFunc (a,b) =

isn't this a function with one curry style argument which happens to be a tuple ? Thus allowing me to pass tuples to this function using the pipleline operator? (where the elements of the tuple is named)

(1,2) |> someFunc

Is this correct?

Roger Johansson
  • 22,764
  • 18
  • 97
  • 193

2 Answers2

7

This will work just fine - the difference is when you have

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

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

let partial = fun t -> f (1,t)
let partial2 = f2 1
John Palmer
  • 25,356
  • 3
  • 48
  • 67
  • Yes, and isnt this simply because "f" has _one_ argument which happens to be a tuple with named elements? I mean you can do funky stuff like "somefunc a b c (d,e,f) g" and this is obviously neither pure curry or typle style, which had me beleive that F# only has curry style, where arguments simply are values or tuples .. is this the correct way to think of it? – Roger Johansson Jul 09 '12 at 09:54
7

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

let someFunc (a,b) = a + b

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

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

Which is also equivalent to

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

The first version, with the pattern matching in the argument itself, is obviously preferable in this instance of simple named bindings.

Do note that F# methods, however, are "tuple style" (this is one of those places where F# glues into standard .NET object oriented features).

Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136