我正在尝试编写一个函数pipe
,该函数具有一系列数学函数,其中pipe [f1,...,fn] x
应该返回f1(f2(...(fn x)))
我将其设置为:
pipe :: [(a -> a)] -> (a -> a)
pipe fs = foldLeft f base fs
where
f a x =
base =
-- >>> pipe [] 3
-- 3
--
-- >>> pipe [(\x -> x+x),(\x -> x + 3)] 3
-- 12
--
-- >>> pipe [(\x -> x * 4),(\x -> x + x)] 3
-- 24
使用foldl进行此操作的最佳方法是什么? 谢谢!