span package:base

span, applied to a predicate p and a list xs, returns a tuple where first element is the longest prefix (possibly empty) of xs of elements that satisfy p and second element is the remainder of the list: span p xs is equivalent to (takeWhile p xs, dropWhile p xs), even if p is _|_.

Laziness

>>> span undefined []
([],[])

>>> fst (span (const False) undefined)
*** Exception: Prelude.undefined

>>> fst (span (const False) (undefined : undefined))
[]

>>> take 1 (fst (span (const True) (1 : undefined)))
[1]
span produces the first component of the tuple lazily:
>>> take 10 (fst (span (const True) [1..]))
[1,2,3,4,5,6,7,8,9,10]

Examples

>>> span (< 3) [1,2,3,4,1,2,3,4]
([1,2],[3,4,1,2,3,4])
>>> span (< 9) [1,2,3]
([1,2,3],[])
>>> span (< 0) [1,2,3]
([],[1,2,3])
span p xs returns the longest prefix of xs that satisfies p, together with the remainder of the stream.
'span' p xs == ('takeWhile' p xs, 'dropWhile' p xs)
xs == ys ++ zs where (ys, zs) = 'span' p xs
Get the source span of a CostCentre.