:: [a] -> [a] -package:extra

Warning: This is a partial function, it throws an error on empty lists. Replace it with drop 1, or use pattern matching or uncons instead. Consider refactoring to use Data.List.NonEmpty.
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
reverse xs returns the elements of xs in reverse order. xs must be finite.

Laziness

reverse is lazy in its elements.
>>> head (reverse [undefined, 1])
1
>>> reverse (1 : 2 : undefined)
*** Exception: Prelude.undefined

Examples

>>> reverse []
[]
>>> reverse [42]
[42]
>>> reverse [2,5,7]
[7,5,2]
>>> reverse [1..]
* Hangs forever *
cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.

Examples

>>> cycle []
*** Exception: Prelude.cycle: empty list
>>> take 10 (cycle [42])
[42,42,42,42,42,42,42,42,42,42]
>>> take 10 (cycle [2, 5, 7])
[2,5,7,2,5,7,2,5,7,2]
>>> take 1 (cycle (42 : undefined))
[42]
A total variant of tail.
A total variant of init.
Identical to tail, namely that fails on an empty list. Useful to avoid the x-partial warning introduced in GHC 9.8.
tailErr [] = error "Prelude.tail: empty list"
tailErr [1,2,3] = [2,3]
tailSafe [] = []
tailSafe [1,3,4] = [3,4]
cycle ties a finite list into a circular one, or equivalently, the infinite repetition of the original list. It is the identity on infinite lists.
>>> cycle []
*** Exception: Prelude.cycle: empty list

>>> cycle [42]
[42,42,42,42,42,42,42,42,42,42...

>>> cycle [2, 5, 7]
[2,5,7,2,5,7,2,5,7,2,5,7...
Extract the elements after the head of a list, which must be non-empty.
>>> tail [1, 2, 3]
[2,3]

>>> tail [1]
[]

>>> tail []
*** Exception: Prelude.tail: empty list
WARNING: This function is partial. You can use case-matching or uncons instead.
Return all the elements of a list except the last one. The list must be non-empty.
>>> init [1, 2, 3]
[1,2]

>>> init [1]
[]

>>> init []
*** Exception: Prelude.init: empty list
WARNING: This function is partial. You can use reverse with case-matching or uncons instead.
reverse xs returns the elements of xs in reverse order. xs must be finite.
>>> reverse []
[]

>>> reverse [42]
[42]

>>> reverse [2,5,7]
[7,5,2]

>>> reverse [1..]
* Hangs forever *
Forces the evaluation of the entire list.
Extract the elements after the head of a list, which must be non-empty.

Examples

>>> tail [1, 2, 3]
[2,3]
>>> tail [1]
[]
>>> tail []
*** Exception: Prelude.tail: empty list
Creates an infinite list from a finite list by appending the list to itself infinite times (i.e. by cycling the list). Unlike cycle from Data.List, this implementation doesn't throw error on empty lists, but returns an empty list instead.
>>> cycle []
[]

>>> take 10 $ cycle [1,2,3]
[1,2,3,1,2,3,1,2,3,1]
Extract the elements after the head of a list, which must be non-empty. To disable the warning about partiality put {-# OPTIONS_GHC -Wno-x-partial -Wno-unrecognised-warning-flags #-} at the top of the file. To disable it throughout a package put the same options into ghc-options section of Cabal file. To disable it in GHCi put :set -Wno-x-partial -Wno-unrecognised-warning-flags into ~/.ghci config file. See also the migration guide.

Examples

>>> tail [1, 2, 3]
[2,3]
>>> tail [1]
[]
>>> tail []
*** Exception: Prelude.tail: empty list