在Haskell中修改了`break`?

Kev*_*ith 3 haskell

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)

Jon*_*rdy 7

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

当元组的工作,不过,我一般喜欢使用secondControl.Arrowfmap,因为它传达的意图好一点.

second (drop 1) $ break (== ' ') "hey there bro"
== ("hey","there bro")
Run Code Online (Sandbox Code Playgroud)

  • @bheklilr Argh你为什么要这么说,我当然要试试吧!每个人,不要尝试在WinGHCi,我不得不使用regedit删除提示设置,所以我可以让它重新开始D: (2认同)