在Haskell中修改了`break`?

时间:2014-05-14 00:47:37

标签: haskell

break有签名[a] -> (a -> Bool) -> ([a], [a]),其中第一个元组等于我理解为takeWhile predicate is true。第二个元组是负责使谓词为假加上剩余列表的项目。

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

但是,是否有一个函数会跳过负责破解的项目?

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

1 个答案:

答案 0 :(得分:7)

不在标准库中,但您可以使用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")