break具有签名[a] -> (a -> Bool) -> ([a], [a]),据我所知,第一个元组等于takeWhile predicate is true。第二元组是负责使谓词为假的项以及其余列表。

> break (== ' ') "hey there bro"
("hey"," there bro")


但是,是否有一个功能可以跳过造成损坏的项目?

>foo? (== ' ') "hey there bro"
("hey","there bro")

最佳答案

不在标准库中,但是您可以使用drop 1实例对方便地在元组的第二个元素上Functor

break (== ' ') "hey there bro"
== ("hey"," there bro")

drop 1 <$> break (== ' ') "hey there bro"
== ("hey","there bro")


<$>fmap的中缀同义词。使用drop 1代替tail处理后缀为空的情况:

drop 1 <$> break (== ' ') "hey"
== ("hey","")

tail <$> break (== ' ') "hey"
== ("hey","*** Exception: Prelude.tail: empty list


但是,在使用元组时,我通常更喜欢使用second中的Control.Arrow而不是fmap,因为它可以更好地传达意图:

second (drop 1) $ break (== ' ') "hey there bro"
== ("hey","there bro")

10-08 12:48