Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

clojure
communitycreator

What is the cons() method in Clojure?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

A list is a data storage structure that holds a collection of data objects. The list() method in Clojure builds lists. You can read this shot about creating a list.

The cons() method is a list method that adds a data item to a list and returns a new list.

Syntax

(cons element lst)

Parameter

We pass two parameters to the cons() method:

lst: This represents the list of items.

element: This represents the data items to be added.

Return value

The cons method will return a list of data items.

Example

(ns clojure.examples.example
   (:gen-class))
(defn Conss []
   (println (cons 6 (list 11, 54,9))))
(Conss)
The cons() method in Clojure

Explanation

  • Line 3: We define a function, Conss.

  • Line 4: We print the new list returned using println. The cons method adds data item 6 to the list.

  • Line 5: We call the Conjj function.

RELATED TAGS

clojure
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring