F# parameter passing

陌路散爱 提交于 2019-12-07 02:39:55

问题


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?


回答1:


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



回答2:


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).



来源:https://stackoverflow.com/questions/11392427/f-parameter-passing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!