init module:List

Return all the elements of a list except the last one. The list must be non-empty. WARNING: This function is partial. Consider using unsnoc instead.

Examples

>>> init [1, 2, 3]
[1,2]
>>> init [1]
[]
>>> init []
*** Exception: Prelude.init: empty list
Extract everything except the last element of the stream.
O(n). Return all the elements of a NonEmpty list except the last one element. Actual type of this function is the following:
init :: NonEmpty a -> [a]
but it was given a more complex type to provide friendlier compile time errors.
>>> init ('a' :| "bcde")
"abcd"

>>> init [0..5 :: Int]
...
... 'init' works with 'NonEmpty', not ordinary lists.
Possible fix:
Replace: [Int]
With:    NonEmpty Int
...
However, you can use 'init' with the ordinary lists.
Apply 'viaNonEmpty' function from relude:
viaNonEmpty init (yourList)
Note, that this will return 'Maybe [Int]'
therefore it is a safe function unlike 'init' from the standard Prelude
...

>>> init (Just 'a')
...
... 'init' works with 'NonEmpty Char' lists
But given: Maybe Char
...
Return all the elements of a list except the last one. The list must be non-empty.
All elements of the list except the last one. See also inits.
init returns all but the last element of the list. Unspecified if the list is empty.
>>> prove $ \(h :: SInteger) t -> init (t ++ singleton h) .== t
Q.E.D.
Safe. O(n).
Return all the elements of a list except the last one. Empty lists throw an EmptyListException.
Return Just the non-last elements of a list.
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]]
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 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 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 :| []) :| []
Drop an initial blank?
This function is lazier than the one suggested in the Haskell 98 report. It is inits undefined = [] : undefined, in contrast to Data.List.inits undefined = undefined.
The inits function returns all initial segments of the argument, shortest first. For example,
>>> inits "abc"
["","a","ab","abc"]
Note that inits has the following strictness property: inits (xs ++ _|_) = inits xs ++ _|_ In particular, inits _|_ = [] : _|_
All initial segments of the list, shortest first
The inits function returns all initial segments of the argument, shortest first. For example,
>>> 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.
init of non-empty list, safe. O(n). init1 a as = init (a:as)
init and last in one go, safe. O(n).