break[a] -> (a -> Bool) -> ([a], [a])根据我的理解,有第一个元组等于的签名takeWhile predicate is true.第二元组是负责使谓词为假加上剩余列表的项目.
> break (== ' ') "hey there bro"
("hey"," there bro")
Run Code Online (Sandbox Code Playgroud)
但是,是否有一个功能会跳过负责破坏的项目?
>foo? (== ' ') "hey there bro"
("hey","there bro")
Run Code Online (Sandbox Code Playgroud)
不在标准库中,但您可以drop 1使用对Functor实例方便地在元组的第二个元素上:
break (== ' ') "hey there bro"
== ("hey"," there bro")
drop 1 <$> break (== ' ') "hey there bro"
== ("hey","there bro")
Run Code Online (Sandbox Code Playgroud)
<$>是中缀的同义词fmap.使用drop 1而不是tail句柄的空后缀:
drop 1 <$> break (== ' ') "hey"
== ("hey","")
tail <$> break (== ' ') "hey"
== ("hey","*** Exception: Prelude.tail: empty list
Run Code Online (Sandbox Code Playgroud)
当元组的工作,不过,我一般喜欢使用second从Control.Arrow过fmap,因为它传达的意图好一点.
second (drop 1) $ break (== ' ') "hey there bro"
== ("hey","there bro")
Run Code Online (Sandbox Code Playgroud)