{:check ["true"],
 :rank
 ["list" "list-creation" "list-destructuring" "list-construction" "list-transform"]}

Index

Functions on Lists

List

Lists

Clojure's list data structure is implemented as a single-linked list.

List-Creation

Functions on Lists

We will survey functions that operate on lists.

Note

  1. Clojure only supports immutable data structures, so these functions can only transform lists, not modify them.

  2. Lists are different from vectors.

In [1]:
; Constructing a list using literals
'(3 2 1 "Hello" "World")
Out[1]:
(3 2 1 "Hello" "World")
In [3]:
; Converting an existing data structure to a list
; `(seq x)` converts `x` to a list whenever possible. 
(seq [3 2 1 "Hello" "World"])
Out[3]:
(3 2 1 "Hello" "World")
In [8]:
; There are also functions that generate lists.
; (range n) constructs a list of integers.
(range 10)
Out[8]:
(0 1 2 3 4 5 6 7 8 9)
In [12]:
; (range m n) is a list of integers from m to (n-1)
(range 5 10)
Out[12]:
(5 6 7 8 9)
In [14]:
; The nil value is treated as an empty list
nil
Out[14]:
nil

List-Destructuring

Destructuring

Destructuring is operations that break up a data structure to its constituent parts.

Let's make a list

In [1]:
(def xs '(3 2 1 "Hello" "World" :again :and :again))
Out[1]:
#'user/xs
In [2]:
; Get the first element
(first xs)
Out[2]:
3
In [3]:
; Get the rest of the list.
(rest xs)
Out[3]:
(2 1 "Hello" "World" :again :and :again)
In [4]:
; Last element of the list.
(last xs)
Out[4]:
:again
In [5]:
; Cherry picking elements
(nth xs 3)
Out[5]:
"Hello"
In [6]:
; Take a few elements from the head list
(take 3 xs)
Out[6]:
(3 2 1)
In [7]:
; Take a few elements from the end of the list
(take-last 3 xs)
Out[7]:
(:again :and :again)
In [8]:
; Remove a few elements from the head of the list
(drop 3 xs)
Out[8]:
("Hello" "World" :again :and :again)
In [9]:
; Remove a few elements from the tail of the list
(drop-last 3 xs)
Out[9]:
(3 2 1 "Hello" "World")

List-Construction

Construction

The following functions create larger lists from smaller parts.

Let's make a list

In [2]:
(def xs '(3 2 1 "Hello" "World" :again :and :again))
Out[2]:
#'user/xs
In [3]:
; Adding one element to the beginning
; (cons element list)
(cons "NEW" xs)
Out[3]:
("NEW" 3 2 1 "Hello" "World" :again :and :again)
In [4]:
; Adding an element in the fastest way.
; (conj list element)
;
; Since we are dealing with linked list, the
; fastest insert is at the head of the list.
(conj xs "NEW")
Out[4]:
("NEW" 3 2 1 "Hello" "World" :again :and :again)
In [5]:
; We can concatenate two lists to form a larger one.
(concat '("*" "**") xs)
Out[5]:
("*" "**" 3 2 1 "Hello" "World" :again :and :again)
In [9]:
; We can use concat to append elements to the tail of the list
(concat xs ["***"])
Out[9]:
(3 2 1 "Hello" "World" :again :and :again "***")
In [8]:
; We can insert multiple elements into the list in the most efficient way.
; For linked list, it's to prepend them at the end of the list.
(into xs ["*" "**"])
Out[8]:
("**" "*" 3 2 1 "Hello" "World" :again :and :again)

List-Transform

A few other functions

Let's make a list

In [1]:
(def xs '(3 2 1 2 7 5 3 4 1))
Out[1]:
#'user/xs
In [2]:
; Count elements
(count xs)
Out[2]:
9
In [3]:
; Count elements in an empty list
(count nil)
Out[3]:
0
In [4]:
; Sort the list using a natural sorting
(sort xs)
Out[4]:
(1 1 2 2 3 3 4 5 7)

Way... more... to come....

Clojure's core strength comes from its rich collection of powerful data transformation functions. We will be exploring way more functions that will work with lists and other data structures.

https://clojure.org/api/cheatsheet