我不認爲有一個很好的方法來將myfunc
的參數設置爲參數func1
和func2
。然而,你的例子也許是有點過於簡單,因爲你可以只寫:
let myfunc x y =
func1 x y && func2 x y
我想,在現實中,你有功能的大數字,然後使用fold
使一個良好的感覺。在這種情況下,您可以使用fold
來組合函數,而不是使用它來合併結果。
如果我簡化問題一點,假設func1
和func2
採取兩個參數作爲一個元組(而不是把他們作爲兩個單獨的參數),那麼你可以寫這樣的事情:
let func1 (a, b) = true
let func2 (a, b) = true
let myfunc = List.fold (fun f st a -> f a && st a) (fun _ -> true) [ func1; func2 ]
現在你不需要明確地傳遞參數(到func1
和func2
),但是fold
的參數有點複雜。我認爲這很好,因爲你需要只寫一次(並且這種方式非常可讀)。
不過,如果你是自由點式的風扇(或只是想看看你能走多遠),你可以定義一些輔助功能,然後寫代碼如下:
/// Given a value, returns a constant function that always returns that value
let constant a _ = a
/// Takes an operation 'a -> b -> c' and builds a function that
/// performs the operation on results of functions
let lift2 op f g x = op (f x) (g x)
let myfunc2 = List.fold (lift2 (&&)) (constant true) [ ffunc1; ffunc2 ]
如果你不需要任意數量的函數,那麼我會簡化代碼,根本不使用fold
。如果您需要這樣做,那麼我認爲您的版本非常易讀,不會太長。我在這個答案中寫的例子表明,你可以避免手工傳遞參數,但它使代碼有點神祕。
相關:http://stackoverflow.com/q/10854010/162396 – Daniel
我也看到了S combinator,有趣的是要了解它真正有用的地方在於它的組成。 –