我目前正在尝试从电子书 Haskell School of Music 中解决这个问题:
定义一个函数 applyAll,给定一个函数列表 [ f1, f2, ..., fn ] 和一个值 v,返回结果 f1 (f2 (...(fn v)...))。
例如: applyAll [简单 2 2, (+3)] 5 ⇒ 20
目前我有
simple :: Integer -> Integer -> Integer -> Integer
simple x y z = x * (y + z)
applyAll :: [(f -> a)] -> a -> a
applyAll [f] v = foldr (.) v [f]
a = (applyAll [simple 2 2, (+3)] 5)
print a
这给了我错误:
Couldn't match type `f' with `a0 -> f'
`f' is a rigid type variable bound by
the type signature for applyAll :: [f -> a] -> a -> a
Expected type: (f -> a) -> a -> a
Actual type: (f -> a) -> (a0 -> f) -> a0 -> a
In the first argument of `foldr', namely `(.)'
In the expression: foldr (.) v [f]
我假设它与类型签名有关,但到目前为止我没有尝试过任何结果。