[英]Higher order function in F#
大家好我想表達函數調用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]))
這是設計的。 函數中定義的值和函數只能在該函數中訪問,從外部看不到它們。
這允許我們定義輔助函數和中間值,而不會污染全局命名空間。
對於功能higher
是功能的外部訪問test1
,你需要之前或之后將其定義test1
,而不是在它里面。 test1
定義的任何內容都只能在test1
訪問。
如果你不想定義higher
但是將函數傳遞給執行相同的test1
,只需傳遞一個函數文字:
printfn "%A" (test1 ((fun a b -> a > b) 5 [5;2;7;8]))
或者,因為在這種情況下,您只是直接比較兩個操作數,甚至更短:
printfn "%A" (test1 ((>) 5 [5;2;7;8]))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.