Ale*_*ett 1 f# functional-programming currying
我对函数式编程有点新意,虽然我对F#有点熟悉,但我仍然在学习所有奇怪的工作方式.
//I love my Rice and Curry'd functions
let add x =
let subFunction y =
x + y
subFunction
//explicit parameter
let add1 y = add 1 y
//implicit parameter
let add10 = add 10
//calling it with the parameter it doesn't show that it takes
let twenty = add10 10
Run Code Online (Sandbox Code Playgroud)
所以这里add10有隐式参数,因为它调用的函数返回一个带参数的函数.为什么我接受我可以这样声明它而不是我声明add1的方式?
从它的声明判断它真的具有欺骗性,人们会假设它只是一个int.
这是来自lambda演算的一种叫做eta-reduction的东西
基本上它意味着你可以通过消除表达式两边的最后一个参数来简化你的函数/ lambda:
// instead of
let f x y = x + y
// in F# you can write
let f x y = (+) x y
// which is also the same as
let f x y = ((+) x) y
// then you can remove y
let f x = (+) x
// and then remove x
let f = (+)
Run Code Online (Sandbox Code Playgroud)
在F#中,只要您没有达到数值限制,就可以使用它.所以在你的代码中let add1 y = add 1 y并且let add10 = add 10是等价的.这是一个示例,说明如何应用逻辑来推理代码,应用于重构.