inits package:base-compat

The inits function takes a stream xs and returns all the finite prefixes of xs, starting with the shortest. The result is NonEmpty because the result always contains the empty list as the first element.
inits [1,2,3] == [] :| [[1], [1,2], [1,2,3]]
inits [1] == [] :| [[1]]
inits [] == [] :| []
The inits1 function returns all non-empty initial segments of the argument, shortest first.

Laziness

Note that inits1 has the following strictness property: inits1 (xs ++ _|_) = inits1 xs ++ _|_ In particular, inits1 _|_ = _|_

Examples

>>> inits1 "abc"
['a' :| "",'a' :| "b",'a' :| "bc"]
>>> inits1 []
[]
inits1 is productive on infinite lists:
>>> take 3 $ inits1 [1..]
[1 :| [],1 :| [2],1 :| [2,3]]
The inits1 function takes a NonEmpty stream xs and returns all the NonEmpty finite prefixes of xs, starting with the shortest.
inits1 (1 :| [2,3]) == (1 :| []) :| [1 :| [2], 1 :| [2,3]]
inits1 (1 :| []) == (1 :| []) :| []