Lambda F中的高阶函数#

Lambda F中的高阶函数#,lambda,f#,functional-programming,Lambda,F#,Functional Programming,大家好,我想表达对test1的函数调用,而不首先定义函数 let higher = fun a b -> a>b let rec test1 test2 number list= match (number,list) with |number,[] -> [] |number,x1::xs when test2 a x = true -> x1::test1 test2

大家好,我想表达对test1的函数调用,而不首先定义函数

let higher = fun a b -> a>b
let rec test1 test2 number list=

        match (number,list) with
        |number,[]                           -> []
        |number,x1::xs when test2 a x = true -> x1::test1 test2 number xs 
        |number,x1::xs                       -> test1 test2 number xs 

printfn "%A" (test1 (higher 5 [5;2;7;8]))

这是故意的。函数中定义的值和函数只能在该函数中访问,从外部看不到

这允许我们在不污染全局名称空间的情况下定义助手函数和中间值


要在函数
test1
外部访问函数
higher
,您需要在
test1
之前或之后定义它,但不能在它内部定义。
test1
中定义的任何内容都只能在
test1

中访问,如果您不想定义
更高的
,而是将一个函数传递到
test1
,该函数也会这样做,只需传递一个函数文本:

printfn "%A" (test1 ((fun a b -> a > b) 5 [5;2;7;8]))
或者,因为在这种情况下,您只是直接比较两个操作数,所以更短:

printfn "%A" (test1 ((>) 5 [5;2;7;8]))

如果a>b,则true-else-false
可以更简洁地写成
a>b
。是的,本地绑定就是这样工作的。它们是本地的,而不是全球的。