为什么隐式参数不被视为 F# 中的编译器错误

Why are implicit parameters not considered compiler errors in F#

我对函数式编程有点陌生,虽然我对 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

所以这里 add10 具有隐式参数,因为它正在调用一个函数 returns 一个接受参数的函数。为什么接受我可以那样声明它而不是我声明 add1 的方式?

从它的声明来看,它确实具有欺骗性,人们会认为它只是一个 int。

这是来自 lambda 演算的东西,叫做 eta-reduction

基本上这意味着您可以通过删除表达式两边相同的最后一个参数来简化 function/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     = (+)

在 F# 中,只要未达到 value restriction,您就可以玩这个。所以在你的代码中 let add1 y = add 1 ylet add10 = add 10 是等价的。这是一个示例,说明如何应用逻辑推理代码,并将其应用于重构。