我一直在尝试实现一个函数,该函数将作为参数给出的函数列表应用于给定变量。我遇到了无限类型和类型不匹配的各种类型错误。下面是使用递归的草稿,我相信已经接近工作,但继续遇到类型不匹配的情况。我陷入了这个问题的泥潭,不知道如何取得进展。
applyList :: [a -> b] -> a -> [b]
applyList [] variable = variable
applyList (f:fs) variable = applyList fs $(\f variable -> f variable)
Run Code Online (Sandbox Code Playgroud)
这是我使用 Foldl 的另一个草稿解决方案,也没有成功。
applyList :: [a -> a] -> a -> a
applyList fs variable = foldl (\variable f -> f variable) variable fs
Run Code Online (Sandbox Code Playgroud)
用法示例:
applyList [] "foo" ==> "foo"
applyList [] 1 ==> 1
applyList [(++"bar")] "foo" ==> "foobar"
applyList [reverse, tail, (++"bar")] "foo" ==> "raboo"
applyList [(3*), (2^), (+1)] 0 ==> 6 …Run Code Online (Sandbox Code Playgroud)