[英]Does Haskell have a takeUntil function?
目前我正在使用
takeWhile (\x -> x /= 1 && x /= 89) l
從列表中獲取元素到1或89.但是,結果不包括這些sentinel值。 Haskell是否有一個標准函數在takeWhile
上提供這種變體,包括結果中的takeWhile
? 到目前為止,我對Hoogle的搜索一直沒有用。
既然你問的是標准功能, 沒有 。 但是也沒有包含takeWhileInclusive
的包,但這很簡單:
takeWhileInclusive :: (a -> Bool) -> [a] -> [a]
takeWhileInclusive _ [] = []
takeWhileInclusive p (x:xs) = x : if p x then takeWhileInclusive p xs
else []
您需要做的唯一事情是獲取值,無論謂詞是否返回True並且僅使用謂詞作為延續因子:
*Main> takeWhileInclusive (\x -> x /= 20) [10..]
[10,11,12,13,14,15,16,17,18,19,20]
span
你想要的?
matching, rest = span (\x -> x /= 1 && x /= 89) l
然后看看rest
的頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.