>>> 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]]
inits (fromList "abc") = fromList [fromList "", fromList "a", fromList "ab", fromList "abc"]Evaluating the <math>th prefix takes <math>, but evaluating every prefix in the sequence takes <math> due to sharing.
> inits [1,2] [[],[1],[1,2]] > inits [] [[]]
>>> :set -XPostfixOperators >>> Data.List.Infinite.take 5 $ Data.List.Infinite.inits (0...) [[],[0],[0,1],[0,1,2],[0,1,2,3]]If you need reversed prefixes, they can be generated cheaper using scanl':
>>> :set -XPostfixOperators >>> Data.List.Infinite.take 5 $ Data.List.Infinite.scanl' (flip (:)) [] (0...) [[],[0],[1,0],[2,1,0],[3,2,1,0]]
tails (fromList (1:|[2,3])) = fromList (fromList (1:|[]) :| [fromList (1:|[2]), fromList (1:|[2,3]))Evaluating the <math>th prefix takes <math>, but evaluating every prefix in the sequence takes <math> due to sharing.
>>> inits "abc" ["","a","ab","abc"]Note that inits has the following strictness property: inits (xs ++ _|_) = inits xs ++ _|_ In particular, inits _|_ = [] : _|_ inits is semantically equivalent to map reverse . scanl (flip (:)) [], but under the hood uses a queue to amortize costs of reverse.
>>> inits enGBTJP [en,en-GB,en-GB-t-jp]