How to get the tail of a list in Haskell

Overview

Have you ever wanted to get all the elements of a list except the first element? In this shot, you will learn how to get all elements of a list excluding the first one.

What is list?

A list in Haskell can be anything ranging from strings, an array of numbers, etc,.

Why would you want the tail of a list?

widget

Remember a list can also be a string of characters so, in situations where you just want the last letters of a word without the first letter, that’s where the tail method will help.

Why does list matter?

List enables you to manipulate arrays and strings. Look up my shot on how to get element of list at index. In that shot, I discuss how to get any element from a list with index. For examples, if you want to get the 3rd letter in the word ‘great’ you will be able to do that with help of a list.

What is the tail of a list?

The tail of a list is everything in the list except the first element of the list. The tail of a list in Haskell is different from the last element of a list.

Syntax

  • tail list: you call the tail before the list.

Parameter

The tail receives one parameter which is the list of element.

Example

main = do
let list = [2,3,4,5,6]
print(tail list)

Explanation

From the example above we use the tail method to get the tail elements of our list. Notice how from the beginning the list had 5 element but when we print it with the tail method, it returned all the elements of the list except the first element.