[英]Does Haskell have a takeUntil function?
Currently I am using 目前我正在使用
takeWhile (\x -> x /= 1 && x /= 89) l
to get the elements from a list up to either a 1 or 89. However, the result doesn't include these sentinel values. 从列表中获取元素到1或89.但是,结果不包括这些sentinel值。 Does Haskell have a standard function that provides this variation on takeWhile
that includes the sentinel in the result? Haskell是否有一个标准函数在takeWhile
上提供这种变体,包括结果中的takeWhile
? My searches with Hoogle have been unfruitful so far. 到目前为止,我对Hoogle的搜索一直没有用。
Since you were asking about standard functions, no . 既然你问的是标准功能, 没有 。 But also there isn't a package containing a takeWhileInclusive
, but that's really simple: 但是也没有包含takeWhileInclusive
的包,但这很简单:
takeWhileInclusive :: (a -> Bool) -> [a] -> [a]
takeWhileInclusive _ [] = []
takeWhileInclusive p (x:xs) = x : if p x then takeWhileInclusive p xs
else []
The only thing you need to do is to take the value regardless whether the predicate returns True and only use the predicate as a continuation factor: 您需要做的唯一事情是获取值,无论谓词是否返回True并且仅使用谓词作为延续因子:
*Main> takeWhileInclusive (\x -> x /= 20) [10..]
[10,11,12,13,14,15,16,17,18,19,20]
Is span
what you want? span
你想要的?
matching, rest = span (\x -> x /= 1 && x /= 89) l
then look at the head of rest
. 然后看看rest
的头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.