我不认为有一个很好的方法来将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,有趣的是要了解它真正有用的地方在于它的组成。 –