What is the rest method in Clojure sequence?

Overview

In Clojure, a sequence holds a collection of data objects and is seen as a logical list. We use the seq method to create a sequence in Clojure.

The rest sequence method returns a sequence containing all the elements except for the first element.

Syntax

(rest seqq)
rest sequence syntax

Parameters

seqq: This represents the sequence containing the element.

Return value

This method returns a sequence containing all the elements except the first one.

Example

(ns clojure.examples.example
(:gen-class))
(defn restt []
(def seq1 (seq [1 2 8 9 1 0 6 8 2]))
(println (rest seq1)))
(restt)

Explanation

  • Line 3: We define a function restt.
  • Line 4: We define our sequence seq1.
  • Line 5: We use the rest method to obtain the last value of the seq1.
  • Line 6: We call the restt function. Observe that the output returned all the elements of the sequence except "1" since it is the first element of the seq1 sequence.

Free Resources