我是Haskell的新手,我正在阅读本书"Real World Haskell"。在本书的第4章中,作者要求使用fold来重写groupBy函数。一书(屋大维沃伊库)的读者提供了以下解决方案:有多少个参数需要Haskell的foldr函数?
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个参数。 为什么会发生这种情况? 谢谢。