F# parameter passing

谁说胖子不能爱 提交于 2019-12-05 06:02:28

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

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

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