Skip to content

Instantly share code, notes, and snippets.

@jordi-petit
Last active January 24, 2025 13:48
Show Gist options
  • Save jordi-petit/b0ba0556fda3d2b288408a7e7f10aff9 to your computer and use it in GitHub Desktop.
Save jordi-petit/b0ba0556fda3d2b288408a7e7f10aff9 to your computer and use it in GitHub Desktop.
Useful list functions in Haskell

Useful list functions in Haskell

head, last

  • Header:

    head, last :: [a] -> a
  • Description:

    • head xs is the first element of the xs list.
    • last xs is the last element of the xs list.

    Error if xs is empty.

  • Examples:

    head [1 .. 4]
    πŸ‘‰ 1
    last [1 .. 4]
    πŸ‘‰ 4

tail, init

  • Header:

    tail, init :: [a] -> [a]
  • Description:

    • tail xs is the xs list without its first element.
    • init xs is the xs list without its last element.

    Error if xs is empty.

  • Examples:

    tail [1..4]
    πŸ‘‰ [2, 3, 4]
    init [1..4]
    πŸ‘‰ [1, 2, 3]

reverse

  • Header:

    reverse :: [a] -> [a]
  • Description:

    reverse xs is the xs list in the reverse order.

  • Examples:

    reverse [1..4]
    πŸ‘‰ [4, 3, 2, 1]

length

  • Header:

    length :: [a] -> Int
  • Description:

    length xs is the number of elements elements in the xs list.

null

  • Header:

    null :: [a] -> Bool
  • Description:

    null xs tells whether the xs list is empty.

elem

  • Header:

    elem :: (Eq a) => a -> [a] -> Bool
  • Description:

    elem x xs tells whether x is contained in the xs list.

(!!)

  • Header:

    (!!) :: [a] -> Int -> a
  • Description:

    xs !! is the i-th element of the xs` list (starting from zero).

maximum, minimum

  • Header:

    maximum, minimum :: (Ord a) => [a] -> a
  • Description:

    • maximum xs is the bigger element from the xs list (not empty!).
    • minimum xs is the smaller element from the xs list (not empty!).

and, or

  • Header:

    and, or :: [Bool] -> Bool
  • Description:

    • and bs is the conjuntion of the bs list of bolleans.
    • or bs is the disjuntion of the bs list of bolleans.

sum, prod

  • Header:

    sum, prod :: [Int] -> Int
  • Description:

    • sum xs is the sum of the elements in the xs list.
    • prod xs is the product of the elements in the xs list.
  • Examples:

    fact n = prod [1 .. n]
    
    fact 5
    πŸ‘‰ 120

take, drop

  • Header:

    take, drop :: Int -> [a] -> [a]
  • Description:

    • take n xs is the prefix of length n of the xs list.
    • drop n xs is the sufix of the xs list when the first n elements are removed.
  • Examples:

    take 3 [1 .. 7]
    πŸ‘‰ [1, 2, 3]
    drop 3 [1 .. 7]
    πŸ‘‰ [4, 5, 6, 7]

zip

  • Header:

    zip :: [a] -> [b] -> [(a, b)]
  • Description:

    zip xs ys is the list that joins, in ordee, each pair of elements from xs and ys. If some list lacks elements, the result is truncated.

  • Examples:

    zip [1, 2, 3] ['a', 'b', 'c']
    πŸ‘‰ [(1, 'a'), (2, 'b'), (3, 'c')]
    zip [1 .. 10] [1 .. 3]
    πŸ‘‰ [(1, 1), (2, 2), (3, 3)]

repeat

  • Header:

    repeat :: a -> [a]
  • Description:

    repeat x is the infinite list whose element are always x.

  • Examples:

    repeat 3
    πŸ‘‰ [3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, ...]
    take 4 (repeat 3)
    πŸ‘‰ [3, 3, 3, 3]

concat

  • Header:

    concat :: [[a]] -> [a]
  • Description:

    concat xs is the list that concatenates all lists in xs.

  • Examples:

    concat [[1, 2, 3], [], [3], [1, 2]]
    πŸ‘‰ [1, 2, 3, 3, 1, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment