Clojure - Sequences cons



Returns a new sequence where ‘x’ is the first element and ‘seq’ is the rest.

Syntax

Following is the syntax.

(cons x seq)

Parameters − ‘x’ is the element which needs to be added to the sequence. ‘seq’ is the sequence list of elements.

Return Value − The new sequence with the appended element.

Example

Following is an example of con in Clojure.

(ns clojure.examples.example
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (println (cons 0 (seq [1 2 3]))))
(Example)

Output

The above program produces the following output.

(0 1 2 3)
clojure_sequences.htm
Advertisements