init package:relude

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. WARNING: This function is partial. Consider using unsnoc instead.

Examples

>>> init [1, 2, 3]
[1,2]
>>> init [1]
[]
>>> init []
*** Exception: Prelude.init: empty 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]]
Repeat a monadic action indefinitely. This is a more type safe version of forever, which has a convenient but unsafe type. Consider the following two examples. In the getIntForever functions, it falsely expects Int as the result of the forever function. But it would need to wait *forever* to get that, and this mistake won't be caught by the type system and compiler:
getIntForever :: IO Int
getIntForever = do
i <- forever $ do ...
pure i
In contrast, using infinitely instead of forever in foo is a type error.
True if the argument is an IEEE infinity or negative infinity