我是 Haskell 的新手,我正在阅读 “Real World Haskell”一书。在本书的第 4 章中,作者要求作为练习使用 fold 重写 groupBy 函数。该书的一位读者(Octavian Voicu)给出了以下解决方案:
theCoolGroupBy :: (a -> a -> Bool) -> [a] -> [[a]]
theCoolGroupBy eq xs = tail $ foldr step (\_ -> [[]]) xs $ (\_ -> False)
where step x acc = \p -> if p x then rest p else []:rest (eq x)
where rest q = let y:ys = acc q in (x:y):ys
我的问题很简单:我知道 foldr 需要 3 个参数:一个函数、一个初始值和一个列表。但在代码的第二行中,foldr 需要 4 个参数。为什么会发生这种情况? 谢谢你。