The
inits function returns all initial segments of the
argument, shortest first.
inits is semantically equivalent to
map
reverse . scanl (flip (:)) [], but under the
hood uses a queue to amortize costs of
reverse.
Laziness
Note that
inits has the following strictness property:
inits (xs ++ _|_) = inits xs ++ _|_
In particular,
inits _|_ = [] : _|_
Examples
>>> inits "abc"
["","a","ab","abc"]
>>> inits []
[[]]
inits is productive on infinite lists:
>>> take 5 $ inits [1..]
[[],[1],[1,2],[1,2,3],[1,2,3,4]]