Haskell 更简单的groupStartBy函数?

Haskell 更简单的groupStartBy函数?,haskell,Haskell,我第二次在不相关的项目中编写以下函数(第一次是XML处理,现在是自定义命令行标志处理),我感觉它应该存在于某个库中,我就是找不到它。它对列表元素进行分组,每组从谓词为true的元素开始 有更简单的方法吗 groupStartBy :: (a -> Bool) -> [a] -> [[a]] groupStartBy pred xs = reverse $ map reverse $ foldl' step [] xs where step as x | pred x

我第二次在不相关的项目中编写以下函数(第一次是XML处理,现在是自定义命令行标志处理),我感觉它应该存在于某个库中,我就是找不到它。它对列表元素进行分组,每组从谓词为true的元素开始

有更简单的方法吗

groupStartBy :: (a -> Bool) -> [a] -> [[a]]
groupStartBy pred xs = reverse $ map reverse $ foldl' step [] xs
  where
    step as x | pred x = [x]:as
    step (a:as) x = (x:a):as
    step [] x = [[x]]

这一点在这里非常有用。我还没有找到与你的函数功能完全相同的东西,但是如果你使用一些基本函数,我敢打赌你会得到你想要的
splitWhen
类似,但删除满足谓词的元素<代码>拆分。whenElt更接近,但将谓词元素分离到新列表的不同元素中

也许,类似于:

groupStartBy :: (a -> Bool) -> [a] -> [[a]]
groupStartBy f = split (dropInitBlank . keepDelimsL . whenElt $ f)

如果我没听错的话<代码>拆分,
dropInitBlank
keepDelimsL
何时开始

您可以使用
groupBy
执行此操作:

import Data.List (groupBy)

groupStartBy :: (a -> Bool) -> [a] -> [[a]]
groupStartBy pred = groupBy (const (not . pred))
-- or in point free style: groupStartBy = groupBy . const . (not .)