我正在尝试自学使用Haskell进行函数式编程。
我很难理解currying和lambda。
此功能生成列表的前缀列表(输出列表的列表)。
foldr (\element accumulator -> [] : map (element:) accumulator) [[]]
我正在尝试将其重写为不带lambda的常规函数,以帮助我了解lambdas的工作原理。我该怎么办?我被困住了。我需要助手功能吗?谢谢。
是的,您将需要一个辅助功能。where
子句是放置这样的助手的好地方。where
子句附加到定义,所以我将需要命名您的函数(我已经命名了inits
)。首先将表达式逐字移开。
inits :: [a] -> [[a]]
inits = foldr helper [[]]
where
helper = \element accumulator -> [] : map (element:) accumulator
然后,您可以将右侧的lambda参数移至左侧的参数绑定,这意味着同一件事:
inits :: [a] -> [[a]]
inits = foldr helper [[]]
where
helper element accumulator = [] : map (element:) accumulator
(您也可以只执行一个参数:
helper element = \accumulator -> [] : map (element:) accumulator
这些都是等效的。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句