是否有内置函数将函数列表一个接一个地应用于值?我目前正在使用它,但它对我来说似乎很不优雅.
-- applyFunctions [(*2), (+3), (*4)] 1 == ((1 * 2) + 3 ) * 4
applyFunctions :: [(a -> a)] -> a -> a
applyFunctions [] x = x
applyFunctions [f] x = f x
applyFunctions (f:fs) x = applyFunctions fs (f x)
Run Code Online (Sandbox Code Playgroud)
您可以使用 foldl (flip ($))
foldl (flip ($)) 1 [(*2), (*3), (*4)] --yields 24
Run Code Online (Sandbox Code Playgroud)