0
# Core Language Features
1
2
Fundamental language constructs including data structures, control flow, functions, and macros that form the foundation of Clojure programming.
3
4
## Capabilities
5
6
### Data Structure Constructors
7
8
Create the core immutable data structures that are central to Clojure programming.
9
10
```clojure { .api }
11
(list & items)
12
;; Creates a new list containing the items
13
;; Returns: clojure.lang.IPersistentList
14
15
(vector & args)
16
;; Creates a vector containing the arguments
17
;; Returns: clojure.lang.IPersistentVector
18
19
(hash-map & keyvals)
20
;; Creates a hash map from alternating keys and values
21
;; keyvals must be even number of arguments: key1 val1 key2 val2 ...
22
;; Returns: clojure.lang.IPersistentMap
23
24
(hash-set & keys)
25
;; Creates a hash set containing the keys
26
;; Returns: clojure.lang.IPersistentSet
27
28
(sorted-map & keyvals)
29
;; Creates a sorted map from alternating keys and values
30
;; Returns: clojure.lang.IPersistentMap
31
32
(sorted-set & keys)
33
;; Creates a sorted set containing the keys
34
;; Returns: clojure.lang.IPersistentSet
35
```
36
37
### Basic Sequence Operations
38
39
Core functions for working with sequences - the fundamental abstraction in Clojure.
40
41
```clojure { .api }
42
(first coll)
43
;; Returns the first item in the collection
44
;; Returns nil if coll is nil or empty
45
;; Returns: Object
46
47
(rest coll)
48
;; Returns a seq of the items after the first
49
;; Returns empty seq if no more items
50
;; Returns: clojure.lang.ISeq
51
52
(next coll)
53
;; Returns a seq of the items after the first
54
;; Returns nil if no more items
55
;; Returns: clojure.lang.ISeq
56
57
(cons x seq)
58
;; Returns a new seq where x is the first element and seq is the rest
59
;; Returns: clojure.lang.ISeq
60
61
(conj coll & xs)
62
;; Returns a new collection with the xs added
63
;; Addition placement depends on collection type
64
;; Returns: same type as coll
65
66
(count coll)
67
;; Returns the number of items in the collection
68
;; Returns: long
69
70
(empty? coll)
71
;; Returns true if coll has no items
72
;; Returns: boolean
73
74
(seq coll)
75
;; Returns a seq on the collection, nil if empty
76
;; Returns: clojure.lang.ISeq or nil
77
```
78
79
### Type Predicates
80
81
Functions to test the type of values.
82
83
```clojure { .api }
84
(nil? x)
85
;; Returns true if x is nil
86
;; Returns: boolean
87
88
(some? x)
89
;; Returns true if x is not nil
90
;; Returns: boolean
91
92
(boolean? x)
93
;; Returns true if x is a Boolean
94
;; Returns: boolean
95
96
(number? x)
97
;; Returns true if x is a Number
98
;; Returns: boolean
99
100
(string? x)
101
;; Returns true if x is a String
102
;; Returns: boolean
103
104
(keyword? x)
105
;; Returns true if x is a Keyword
106
;; Returns: boolean
107
108
(symbol? x)
109
;; Returns true if x is a Symbol
110
;; Returns: boolean
111
112
(fn? x)
113
;; Returns true if x is a function
114
;; Returns: boolean
115
116
(coll? x)
117
;; Returns true if x is a collection
118
;; Returns: boolean
119
120
(sequential? x)
121
;; Returns true if x is a sequential collection
122
;; Returns: boolean
123
```
124
125
### Collection Type Predicates
126
127
Specific predicates for collection types.
128
129
```clojure { .api }
130
(list? x)
131
;; Returns true if x is a list
132
;; Returns: boolean
133
134
(vector? x)
135
;; Returns true if x is a vector
136
;; Returns: boolean
137
138
(map? x)
139
;; Returns true if x is a map
140
;; Returns: boolean
141
142
(set? x)
143
;; Returns true if x is a set
144
;; Returns: boolean
145
146
(seq? x)
147
;; Returns true if x implements ISeq
148
;; Returns: boolean
149
```
150
151
### Function Definition
152
153
Core constructs for defining functions.
154
155
```clojure { .api }
156
(fn name? [params*] exprs*)
157
;; Creates a function
158
;; name is optional for recursion
159
;; Multiple arity overloads supported: (fn ([x] ...) ([x y] ...))
160
;; Returns: clojure.lang.IFn
161
162
(defn name doc-string? attr-map? [params*] exprs*)
163
;; Defines a function and binds it to a var
164
;; Supports multiple arity: (defn f ([x] ...) ([x y] ...))
165
;; Returns: clojure.lang.Var
166
167
(defn- name doc-string? attr-map? [params*] exprs*)
168
;; Defines a private function
169
;; Returns: clojure.lang.Var
170
```
171
172
### Variables and Binding
173
174
Variable definition and binding constructs.
175
176
```clojure { .api }
177
(def symbol doc-string? init?)
178
;; Creates a global var
179
;; Returns: clojure.lang.Var
180
181
(let [bindings*] exprs*)
182
;; Creates local bindings
183
;; bindings are [name init-expr name init-expr ...]
184
;; Returns: result of last expr
185
186
(binding [bindings*] exprs*)
187
;; Dynamically binds vars during execution
188
;; bindings are [var-symbol value var-symbol value ...]
189
;; Returns: result of last expr
190
```
191
192
### Logical Operations
193
194
Boolean logic and comparison operations.
195
196
```clojure { .api }
197
(not x)
198
;; Returns true if x is logical false (nil or false)
199
;; Returns: boolean
200
201
(and exprs*)
202
;; Evaluates exprs left to right, returns first falsy or last value
203
;; Returns: Object
204
205
(or exprs*)
206
;; Evaluates exprs left to right, returns first truthy value
207
;; Returns: Object
208
209
(= x y & more)
210
;; Returns true if all arguments are equal
211
;; Returns: boolean
212
213
(not= x y & more)
214
;; Returns true if arguments are not all equal
215
;; Returns: boolean
216
217
(< x y & more)
218
;; Returns true if numbers/strings are in monotonically increasing order
219
;; Returns: boolean
220
221
(> x y & more)
222
;; Returns true if numbers/strings are in monotonically decreasing order
223
;; Returns: boolean
224
225
(<= x y & more)
226
;; Returns true if numbers/strings are in monotonically non-decreasing order
227
;; Returns: boolean
228
229
(>= x y & more)
230
;; Returns true if numbers/strings are in monotonically non-increasing order
231
;; Returns: boolean
232
```
233
234
**Usage Examples:**
235
236
```clojure
237
;; Creating data structures
238
(def my-list (list 1 2 3 4))
239
(def my-vector [1 2 3 4]) ; literal syntax
240
(def my-map {:name "Alice" :age 30}) ; literal syntax
241
(def my-set #{1 2 3 4}) ; literal syntax
242
243
;; Working with sequences
244
(first my-vector) ; => 1
245
(rest my-vector) ; => (2 3 4)
246
(cons 0 my-vector) ; => (0 1 2 3 4)
247
(conj my-vector 5) ; => [1 2 3 4 5]
248
249
;; Type checking
250
(vector? [1 2 3]) ; => true
251
(map? {:a 1}) ; => true
252
(nil? nil) ; => true
253
(number? 42) ; => true
254
255
;; Function definition
256
(defn greet [name]
257
(str "Hello, " name "!"))
258
259
(defn add
260
([x y] (+ x y))
261
([x y z] (+ x y z)))
262
263
;; Local bindings
264
(let [x 10
265
y 20]
266
(+ x y)) ; => 30
267
```