Clojure – Lists

Clojure strings lists

In this guide, we will discuss Clojure Lists. List is a structure used to store a collection of data items. In Clojure, the List implements the ISeq interface. Lists are created in Clojure by using the list function.

Example

Following is an example of creating a list of numbers in Clojure.

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list 1 2 3 4)))
(example)

Output

The above code produces the following output.

(1 2 3 4)

Following is an example of creating a list of characters in Clojure.

(ns clojure.examples.example
   (:gen-class))
(defn example []
   (println (list 'a 'b 'c 'd)))
(example)

The above code produces the following output.

(a b c d)

Following are the list methods available in Clojure.

Sr.No.Lists & Description
1list*
Creates a new list containing the items prepended to the rest, the last of which will be treated as a sequence.
2first
This function returns the first item in the list.
3nth
This function returns the item in the ‘nth’ position in the list.
4cons
Returns a new list wherein an element is added to the beginning of the list.
5conj
Returns a new list wherein the list is at the beginning and the elements to be appended are placed at the end.
6rest
Returns the remaining items in the list after the first item.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply