我在無關項目中第二次寫入以下函數(首先是XML處理,現在是自定義命令行標記處理),並且我有一種感覺,它應該存在於某個庫中,我無法找到它。它將列表元素分組,每個組從謂詞爲真的元素開始。更簡單的groupStartBy函數?
任何更簡單的方法來做到這一點?
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]]
尼斯和簡單的選擇! –