lasp 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +30 -0
- data/DOCUMENTATION.md +942 -0
- data/EXAMPLES.md +57 -34
- data/README.md +7 -92
- data/bin/lasp +1 -1
- data/lib/lasp.rb +4 -2
- data/lib/lasp/corelib.rb +24 -22
- data/lib/lasp/errors.rb +4 -1
- data/lib/lasp/ext.rb +33 -0
- data/lib/lasp/fn.rb +2 -2
- data/lib/lasp/interpreter.rb +79 -0
- data/lib/lasp/params.rb +17 -8
- data/lib/lasp/parser.rb +22 -12
- data/lib/lasp/repl.rb +50 -29
- data/lib/lasp/stdlib.lasp +113 -109
- data/lib/lasp/stdmacros.lasp +31 -0
- data/lib/lasp/version.rb +1 -1
- metadata +5 -5
- data/lib/lasp/eval.rb +0 -66
- data/lib/lasp/parameters.rb +0 -93
- data/lib/lasp/representation.rb +0 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 09308a13fbaaa0866f4e0f71c3e9bda5b3f68175
|
4
|
+
data.tar.gz: 8dc8898d8ad6b1eef27801d566a8b963ab7186cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1df013030f0c92ed76d5cf748d640007b9e11446099d9b68ca0c65ce0247743b8c99013a0efa4fc14db22e795372e87462e746b944ce3cd72406aa02c8549740
|
7
|
+
data.tar.gz: a7725ebe46569683d44f88031665bbbb987d68e462fb459a44b1b64e0c05b3d292f00190df9e7bd95be10768e881a63397f341daa483fdd2bda9fe9d33a6ab92
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,35 @@
|
|
1
1
|
# Läsp changelog
|
2
2
|
|
3
|
+
## v0.9.0 - 2016-02-20
|
4
|
+
|
5
|
+
### Added
|
6
|
+
|
7
|
+
- Add support for let-bindings
|
8
|
+
- Make list and dict be functions of their contents
|
9
|
+
- Additions to core library
|
10
|
+
- `print`
|
11
|
+
- `readln`
|
12
|
+
- Additions to standard library
|
13
|
+
- `prompt`
|
14
|
+
- `every`
|
15
|
+
- `->integer`
|
16
|
+
- `->decimal`
|
17
|
+
|
18
|
+
### Changed
|
19
|
+
|
20
|
+
- Make `require` use relative paths
|
21
|
+
- Rename float type to "decimal"
|
22
|
+
- Rename type string to "text"
|
23
|
+
- Allow any non-whitespace in symbol-style strings
|
24
|
+
- Raise `Lasp::NameError` instead of `KeyError` when unable to resolve symbol
|
25
|
+
- Show error class in REPL output
|
26
|
+
|
27
|
+
### Fixed
|
28
|
+
|
29
|
+
- Consistently do not capitalise error messages
|
30
|
+
- Properly enforce that def only be used with a symbol
|
31
|
+
- Properly enforce that parameters are declared as a list
|
32
|
+
|
3
33
|
## v0.8.0 - 2016-02-04
|
4
34
|
|
5
35
|
### Added
|
data/DOCUMENTATION.md
ADDED
@@ -0,0 +1,942 @@
|
|
1
|
+
# Documentation
|
2
|
+
|
3
|
+
## Data types
|
4
|
+
|
5
|
+
| Data type | Example literals | Description |
|
6
|
+
| :--- | :--- | :--- |
|
7
|
+
| integer | `1`, `-42`, `0` | A whole number of any size |
|
8
|
+
| decimal | `3.56`, `-0.5` | A decimal number of any size |
|
9
|
+
| boolean | `true`, `false` | The value `true` or `false` |
|
10
|
+
| nil | `nil` | The "null" or "no value" type |
|
11
|
+
| text | `"some text"`, `:text` | Text of any length. When containing no whitespace, it can be written with a leading colon (handy in dicts) |
|
12
|
+
| list | `(list 1 2 3.5)` | A heterogeneous (allows mixed types) list of values, can be created with the [list](#list) function |
|
13
|
+
| dict | `(dict :one 1 :two 2)` | A dictionary (a.k.a. hash-map) of keys that map to values, can be created with the [dict](#dict) function |
|
14
|
+
| symbol | `symbol`, `'symbol` | Used to name other things (see [def](#def)), but can be obtained in itself by quoting (see [quote](#quote)) |
|
15
|
+
|
16
|
+
|
17
|
+
## Special forms
|
18
|
+
|
19
|
+
### def
|
20
|
+
|
21
|
+
Defines a symbol in the global environment.
|
22
|
+
|
23
|
+
Parameters `(symbol value)`:
|
24
|
+
|
25
|
+
1. The name to refer to the value as a symbol (no other type is allowed).
|
26
|
+
2. The value to refer to, can be almost anything.
|
27
|
+
|
28
|
+
```clojure
|
29
|
+
(def five 5)
|
30
|
+
five ; => 5
|
31
|
+
```
|
32
|
+
|
33
|
+
You **can** redefine symbols at any time, which is especially handy when
|
34
|
+
playing around in the REPL, however you **should** in most cases only define
|
35
|
+
something once and treat it as a constant for all intents and purposes. To
|
36
|
+
create a local binding, use [let](#let) instead.
|
37
|
+
|
38
|
+
|
39
|
+
### fn
|
40
|
+
|
41
|
+
Creates a function.
|
42
|
+
|
43
|
+
Parameters `(parameters body)`
|
44
|
+
|
45
|
+
1. A list of parameters that the function shall accept, e.g. `(arg1 arg2)`, `(one two & others)`, `(& args)`
|
46
|
+
2. The body of the function, the declared parameters will be available in here.
|
47
|
+
|
48
|
+
```clojure
|
49
|
+
; This creates a function object, it can be read as "a function of x".
|
50
|
+
(fn (x) (+ x 2)) ; => #<Fn (x)>
|
51
|
+
|
52
|
+
; Functions are called when placed as the first item in a form:
|
53
|
+
((fn (x) (+ x 2)) 40) ; => 42
|
54
|
+
```
|
55
|
+
|
56
|
+
Most of the time, you'll want to define a function before using it (see [defn](#defn)):
|
57
|
+
|
58
|
+
```clojure
|
59
|
+
(def plus-two (fn (x) (+ x 2)))
|
60
|
+
(plus-two 40) ; => 42
|
61
|
+
```
|
62
|
+
|
63
|
+
Functions will enforce that the correct number of arguments is passed to them:
|
64
|
+
|
65
|
+
```clojure
|
66
|
+
; Here we pass 2 arguments to a 1-arity function, and we get an error:
|
67
|
+
(plus-two 40 41) ; !> Lasp::ArgumentError: wrong number of arguments (2 for 1)
|
68
|
+
```
|
69
|
+
|
70
|
+
Functions can also accept any number of extra arguments as a list:
|
71
|
+
|
72
|
+
```clojure
|
73
|
+
(def show-args
|
74
|
+
(fn (one & others)
|
75
|
+
(list one others)))
|
76
|
+
|
77
|
+
; All arguments after the fixed first one will be passed in as a list:
|
78
|
+
(show-args 1 2 3) ; => (1 (2 3))
|
79
|
+
|
80
|
+
; If only the arguments before the & are passed in, it will be an empty list:
|
81
|
+
(show-args 1) ; => (1 ())
|
82
|
+
|
83
|
+
; The arguments before the & are compulsory:
|
84
|
+
(show-args) ; !> Lasp::ArgumentError: wrong number of arguments (0 for 1+)
|
85
|
+
```
|
86
|
+
|
87
|
+
|
88
|
+
### do
|
89
|
+
|
90
|
+
Executes multiple forms in order and returns the result of the last one.
|
91
|
+
This is useful when you need to have a single form that does several things.
|
92
|
+
|
93
|
+
```clojure
|
94
|
+
; This form will both print "hello" and return 3:
|
95
|
+
(do (println "hello") (+ 1 2))
|
96
|
+
; hello
|
97
|
+
; => 3
|
98
|
+
```
|
99
|
+
|
100
|
+
### if
|
101
|
+
|
102
|
+
If the first form is truthy, evaluates the second form, otherwise it evaluates the third form.
|
103
|
+
|
104
|
+
Parameters `(test true-form false-form)`:
|
105
|
+
|
106
|
+
1. Always evaluated to test which form to proceed with.
|
107
|
+
2. Evaluated only when the first argument is truthy.
|
108
|
+
3. Evaluated only when the first argument is falsy.
|
109
|
+
|
110
|
+
```clojure
|
111
|
+
(if (= 1 1)
|
112
|
+
"yep!"
|
113
|
+
(println "not evaled!")) ; => "yep!"
|
114
|
+
```
|
115
|
+
|
116
|
+
|
117
|
+
### quote
|
118
|
+
|
119
|
+
Returns a form as is, without evaluating it.
|
120
|
+
|
121
|
+
```clojure
|
122
|
+
f ; !> Lasp::NameError: f is not present in this context
|
123
|
+
(quote f) ; => f
|
124
|
+
(quote (f 1 2)) ; => (f 1 2)
|
125
|
+
|
126
|
+
; Quote ignores any extra arguments
|
127
|
+
(quote f g) ; => f
|
128
|
+
```
|
129
|
+
|
130
|
+
You can also use the shorthand syntax `'` to quote forms:
|
131
|
+
|
132
|
+
```clojure
|
133
|
+
'f ; => f
|
134
|
+
'(f 1 2) ; => (f 1 2)
|
135
|
+
```
|
136
|
+
|
137
|
+
There is no difference between lists that you use to store data and the lists
|
138
|
+
that make up your program, therefore quoting a form and calling the
|
139
|
+
[`list`](#list) function have the same effect:
|
140
|
+
|
141
|
+
```clojure
|
142
|
+
(list 1 2 3) ; => (1 2 3)
|
143
|
+
'(1 2 3) ; => (1 2 3)
|
144
|
+
```
|
145
|
+
|
146
|
+
These are both lists to start with, the difference is that the first one is
|
147
|
+
evaluated and the `list` function returns a list of its arguments, and the
|
148
|
+
second one is simply not evaluated and the list is returned as is.
|
149
|
+
|
150
|
+
|
151
|
+
### macro
|
152
|
+
|
153
|
+
Macros can restructure forms before they are evaluated - they work like
|
154
|
+
functions but accept their arguments before evaluation. Instead the result of
|
155
|
+
the macro is evaluated.
|
156
|
+
|
157
|
+
Functions:
|
158
|
+
|
159
|
+
1. Evaluate all the arguments in order.
|
160
|
+
2. Call the function with the arguments.
|
161
|
+
3. Return the result.
|
162
|
+
|
163
|
+
Macros:
|
164
|
+
|
165
|
+
1. Call the macro with the forms it was given as arguments.
|
166
|
+
2. Evaluate the return value of the macro.
|
167
|
+
3. Return the result.
|
168
|
+
|
169
|
+
```clojure
|
170
|
+
(def infix
|
171
|
+
; Accept arguments in natural order for maths operators
|
172
|
+
(macro (form)
|
173
|
+
; Return a list of arguments in correct prefix-notation to be run
|
174
|
+
(list (second form) (first form) (last form))))
|
175
|
+
|
176
|
+
; This will be restructured to `(+ 4 5)` before it is run.
|
177
|
+
(infix (4 + 5)) ; => 9
|
178
|
+
```
|
179
|
+
|
180
|
+
Except for the order of evaluation, macros behave just like functions and can
|
181
|
+
accept rest-arguments etc. the same way. Just like functions, you mostly want
|
182
|
+
to define them before you use them, see [defm](#defm).
|
183
|
+
|
184
|
+
**It is important to quote things that you do not want evaluated until after the macro has returned.**
|
185
|
+
|
186
|
+
```clojure
|
187
|
+
; Note that we want `if` to be evaluated after the macro has returned, so we have to quote it.
|
188
|
+
(def reverse-if
|
189
|
+
(macro (test false-form true-form)
|
190
|
+
(list 'if test true-form false-form)))
|
191
|
+
|
192
|
+
(if true "yes" "no") ; => "yes"
|
193
|
+
(reverse-if true "yes" "no") ; => "no"
|
194
|
+
```
|
195
|
+
|
196
|
+
To debug macros and see what they expand to without trying to evaluate the result, see [macroexpand](#macroexpand).
|
197
|
+
|
198
|
+
|
199
|
+
## Core library
|
200
|
+
|
201
|
+
### +
|
202
|
+
|
203
|
+
Adds arguments in order.
|
204
|
+
|
205
|
+
```clojure
|
206
|
+
(+ 1 2 3) ; => 6
|
207
|
+
```
|
208
|
+
|
209
|
+
|
210
|
+
### -
|
211
|
+
|
212
|
+
Subtracts arguments in order.
|
213
|
+
|
214
|
+
```clojure
|
215
|
+
(- 1 2 3) ; => -4
|
216
|
+
```
|
217
|
+
|
218
|
+
|
219
|
+
### *
|
220
|
+
|
221
|
+
Multiplies arguments in order.
|
222
|
+
|
223
|
+
```clojure
|
224
|
+
(* 2 3 4) ; => 24
|
225
|
+
```
|
226
|
+
|
227
|
+
|
228
|
+
### /
|
229
|
+
|
230
|
+
Divides arguments in order.
|
231
|
+
|
232
|
+
```clojure
|
233
|
+
(/ 20 2 2) ; => 5
|
234
|
+
```
|
235
|
+
|
236
|
+
|
237
|
+
### <
|
238
|
+
|
239
|
+
Mandatory increasing order.
|
240
|
+
|
241
|
+
```clojure
|
242
|
+
(< 20 10) ; => false
|
243
|
+
(< 10 11 12) ; => true
|
244
|
+
(< 10 11 10) ; => false
|
245
|
+
(< 10 10) ; => false
|
246
|
+
```
|
247
|
+
|
248
|
+
|
249
|
+
### >
|
250
|
+
|
251
|
+
Mandatory decreasing order.
|
252
|
+
|
253
|
+
```clojure
|
254
|
+
(> 20 10) ; => true
|
255
|
+
(> 10 9 8) ; => true
|
256
|
+
(> 10 9 10) ; => false
|
257
|
+
(> 10 10) ; => false
|
258
|
+
```
|
259
|
+
|
260
|
+
|
261
|
+
### =
|
262
|
+
|
263
|
+
Equality.
|
264
|
+
|
265
|
+
```clojure
|
266
|
+
(= 20 20 2) ; => false
|
267
|
+
(= 20 20 20) ; => true
|
268
|
+
(= 20) ; => true
|
269
|
+
```
|
270
|
+
|
271
|
+
|
272
|
+
### <=
|
273
|
+
|
274
|
+
Mandatory non-decreasing order.
|
275
|
+
|
276
|
+
```clojure
|
277
|
+
(<= 20 20 30) ; => true
|
278
|
+
(<= 20 20 10) ; => false
|
279
|
+
(<= 20 20) ; => true
|
280
|
+
```
|
281
|
+
|
282
|
+
|
283
|
+
### >=
|
284
|
+
|
285
|
+
Mandatory non-increasing order.
|
286
|
+
|
287
|
+
```clojure
|
288
|
+
(>= 20 20 30) ; => false
|
289
|
+
(>= 20 20 10) ; => true
|
290
|
+
(>= 20 20) ; => true
|
291
|
+
```
|
292
|
+
|
293
|
+
|
294
|
+
### list
|
295
|
+
|
296
|
+
Creates a list of all of its arguments.
|
297
|
+
|
298
|
+
```clojure
|
299
|
+
(list 1 2 3) ; => (1 2 3)
|
300
|
+
|
301
|
+
; Allows mixing arguments in any way
|
302
|
+
(list true nil :thing) ; => (true nil "thing")
|
303
|
+
```
|
304
|
+
|
305
|
+
See [head](#head), [tail](#tail) and [get](#get) for relevant operations (there
|
306
|
+
are many others too).
|
307
|
+
|
308
|
+
Instead of using `get`, lists also act as a function of their contents,
|
309
|
+
allowing for this syntax:
|
310
|
+
|
311
|
+
```clojure
|
312
|
+
(def my-list (list 32 42 6 9))
|
313
|
+
(my-list 1) ; => 42
|
314
|
+
```
|
315
|
+
|
316
|
+
### head
|
317
|
+
|
318
|
+
Gets the first item in a list, otherwise `nil`.
|
319
|
+
|
320
|
+
```clojure
|
321
|
+
(head (list 1 2 3)) ; => 1
|
322
|
+
(head (list)) ; => nil
|
323
|
+
```
|
324
|
+
|
325
|
+
|
326
|
+
### tail
|
327
|
+
|
328
|
+
Gets all items in a list except the first, when empty it returns an empty list.
|
329
|
+
|
330
|
+
```clojure
|
331
|
+
(tail (list 1 2 3)) ; => (2 3)
|
332
|
+
(tail (list)) ; => ()
|
333
|
+
```
|
334
|
+
|
335
|
+
|
336
|
+
### cons
|
337
|
+
|
338
|
+
Pushes an item onto the front of a list. Does **not** change the original list.
|
339
|
+
|
340
|
+
```clojure
|
341
|
+
(cons 1 (list 2 3)) ; => (1 2 3)
|
342
|
+
(cons 1 ()) ; => (1)
|
343
|
+
```
|
344
|
+
|
345
|
+
|
346
|
+
### dict
|
347
|
+
|
348
|
+
Creates a dictionary. The entries are given in pairs directly following each
|
349
|
+
other, you can use newlines to make this clearer. An uneven number of arguments
|
350
|
+
will produce an error.
|
351
|
+
|
352
|
+
```clojure
|
353
|
+
(dict :one 1 :two 2) ; => {"one" 1, "two" 2}
|
354
|
+
|
355
|
+
; Use whitespace to make the keys and values look paired together
|
356
|
+
(dict :one 1
|
357
|
+
:two 2
|
358
|
+
:three 3) ; => {"one" 1, "two" 2, "three" 3}
|
359
|
+
|
360
|
+
(dict :one 1 :two) ; !> ArgumentError: odd number of arguments for Hash
|
361
|
+
```
|
362
|
+
|
363
|
+
See [get](#get), [assoc](#assoc) and [dissoc](#dissoc) for relevant operations.
|
364
|
+
|
365
|
+
Instead of using `get`, dicts also act as a function of their contents,
|
366
|
+
allowing for this syntax:
|
367
|
+
|
368
|
+
```clojure
|
369
|
+
(def my-dict (dict :seven 7))
|
370
|
+
(my-dict :seven) ; => 7
|
371
|
+
```
|
372
|
+
|
373
|
+
|
374
|
+
### get
|
375
|
+
|
376
|
+
Get item by id in a list or dict.
|
377
|
+
|
378
|
+
```clojure
|
379
|
+
(get 0 (list 1 2 3)) ; => 1
|
380
|
+
(get 3 (list 1 2 3)) ; => nil
|
381
|
+
|
382
|
+
(get :one (dict :one 1 :two 2)) ; => 1
|
383
|
+
(get :three (dict :one 1 :two 2)) ; => nil
|
384
|
+
```
|
385
|
+
|
386
|
+
|
387
|
+
### assoc
|
388
|
+
|
389
|
+
Add or change item in dict. Does **not** change the original dict.
|
390
|
+
|
391
|
+
```clojure
|
392
|
+
(assoc (dict) :one 1) ; => {"one" 1}
|
393
|
+
(assoc (dict :one 1) :one 2) ; => {"one" 2}
|
394
|
+
|
395
|
+
; It does NOT change the original data structure
|
396
|
+
(def data (dict :one 1)) ; => {"one" 1}
|
397
|
+
(assoc data :one 2) ; => {"one" 2}
|
398
|
+
data ; => {"one" 1}
|
399
|
+
|
400
|
+
; Works on lists too
|
401
|
+
(assoc (list 1 2 3) 1 5) ; => (1 5 3)
|
402
|
+
(assoc (list 1 2 3) :one 1) ; !> TypeError: no implicit conversion of String into Integer
|
403
|
+
```
|
404
|
+
|
405
|
+
|
406
|
+
### dissoc
|
407
|
+
|
408
|
+
Remove item from dict. Does **not** change the original dict.
|
409
|
+
|
410
|
+
```clojure
|
411
|
+
(dissoc (dict :one 1 :two 2) :one) ; => {"two" 2}
|
412
|
+
|
413
|
+
; It does NOT change the original data structure
|
414
|
+
(def data (dict :one 1 :two 2)) ; => {"one" 1, "two" 2}
|
415
|
+
(dissoc data :one) ; => {"two" 2}
|
416
|
+
data ; => {"one" 1, "two" 2}
|
417
|
+
```
|
418
|
+
|
419
|
+
|
420
|
+
### not
|
421
|
+
|
422
|
+
Returns the inverted truthiness of its argument.
|
423
|
+
|
424
|
+
```clojure
|
425
|
+
(not true) ; => false
|
426
|
+
(not false) ; => true
|
427
|
+
(not nil) ; => true
|
428
|
+
(not 1) ; => false
|
429
|
+
```
|
430
|
+
|
431
|
+
|
432
|
+
### print
|
433
|
+
|
434
|
+
Prints its argument to stdout and returns `nil`.
|
435
|
+
|
436
|
+
```clojure
|
437
|
+
(do
|
438
|
+
(print "Hello ")
|
439
|
+
(print "world!")) ; => nil
|
440
|
+
|
441
|
+
;; Hello world!
|
442
|
+
```
|
443
|
+
|
444
|
+
|
445
|
+
### println
|
446
|
+
|
447
|
+
Prints its argument and a newline to stdout and returns `nil`.
|
448
|
+
|
449
|
+
```clojure
|
450
|
+
(println "Hello World!") ; => nil
|
451
|
+
;; Hello World!
|
452
|
+
```
|
453
|
+
|
454
|
+
|
455
|
+
### readln
|
456
|
+
|
457
|
+
Reads one line of input from the terminal. Halts the program until a newline
|
458
|
+
has been received, the newline character will **not** be part of the returned
|
459
|
+
text.
|
460
|
+
|
461
|
+
```clojure
|
462
|
+
(def input (readln)) ; Will pause here until the user has entered something
|
463
|
+
```
|
464
|
+
|
465
|
+
|
466
|
+
### apply
|
467
|
+
|
468
|
+
Applies a function to a list of arguments as if they were passed in directly.
|
469
|
+
|
470
|
+
```clojure
|
471
|
+
(apply + (list 1 2 3)) ; => 6
|
472
|
+
|
473
|
+
; Equal to this:
|
474
|
+
(+ 1 2 3)
|
475
|
+
```
|
476
|
+
|
477
|
+
|
478
|
+
### .
|
479
|
+
|
480
|
+
Interoperability operator - calls a Ruby method.
|
481
|
+
|
482
|
+
Parameters `(object message & args)`:
|
483
|
+
|
484
|
+
1. The object to send the message to
|
485
|
+
2. A text with the message name
|
486
|
+
3. Any number of arguments to be passed along with the message
|
487
|
+
|
488
|
+
```clojure
|
489
|
+
(. "01011101" :to_i 2) ; => 93
|
490
|
+
```
|
491
|
+
|
492
|
+
```ruby
|
493
|
+
# Equal to this Ruby code:
|
494
|
+
"01011101".to_i(2)
|
495
|
+
```
|
496
|
+
|
497
|
+
|
498
|
+
### require
|
499
|
+
|
500
|
+
Loads and runs a Läsp file. Paths are relative to the file they are being
|
501
|
+
required in.
|
502
|
+
|
503
|
+
```clojure
|
504
|
+
(require "lasp_file.lasp") ; lasp_file.lasp is in the same directory as this code
|
505
|
+
(require "dir/lasp_file.lasp") ; dir/ is a folder with a lasp file in this directory
|
506
|
+
(require "../lasp_file.lasp") ; lasp_file.lasp is in the parent folder
|
507
|
+
```
|
508
|
+
|
509
|
+
|
510
|
+
## Standard library
|
511
|
+
|
512
|
+
### first
|
513
|
+
|
514
|
+
Alias of [head](#head)
|
515
|
+
|
516
|
+
|
517
|
+
### rest
|
518
|
+
|
519
|
+
Alias of [tail](#tail)
|
520
|
+
|
521
|
+
|
522
|
+
### inc
|
523
|
+
|
524
|
+
Increments its argument by 1.
|
525
|
+
|
526
|
+
```clojure
|
527
|
+
(inc 5) ; => 6
|
528
|
+
```
|
529
|
+
|
530
|
+
|
531
|
+
### dec
|
532
|
+
|
533
|
+
Decrements its argument by 1.
|
534
|
+
|
535
|
+
```clojure
|
536
|
+
(dec 5) ; => 4
|
537
|
+
```
|
538
|
+
|
539
|
+
|
540
|
+
### nil?
|
541
|
+
|
542
|
+
Whether its argument is exactly `nil`.
|
543
|
+
|
544
|
+
```clojure
|
545
|
+
(nil? (list)) ; => false
|
546
|
+
(nil? false) ; => false
|
547
|
+
(nil? nil) ; => true
|
548
|
+
```
|
549
|
+
|
550
|
+
|
551
|
+
### empty?
|
552
|
+
|
553
|
+
Whether the given list is empty.
|
554
|
+
|
555
|
+
```clojure
|
556
|
+
(empty? (list 1)) ; => false
|
557
|
+
(empty? (list)) ; => true
|
558
|
+
```
|
559
|
+
|
560
|
+
|
561
|
+
### not=
|
562
|
+
|
563
|
+
Whether not all of the arguments are equal.
|
564
|
+
|
565
|
+
```clojure
|
566
|
+
(not= 2 2 3) ; => true
|
567
|
+
(not= 2 2 2) ; => false
|
568
|
+
```
|
569
|
+
|
570
|
+
|
571
|
+
### second
|
572
|
+
|
573
|
+
The second element in a list, otherwise `nil`.
|
574
|
+
|
575
|
+
```clojure
|
576
|
+
(second (list 1 2 3)) ; => 2
|
577
|
+
(second (list 1)) ; => nil
|
578
|
+
```
|
579
|
+
|
580
|
+
|
581
|
+
### mod
|
582
|
+
|
583
|
+
The remainder (or modulus) of the first argument divided by the second.
|
584
|
+
|
585
|
+
```clojure
|
586
|
+
(mod 38 7) ; => 3
|
587
|
+
```
|
588
|
+
|
589
|
+
|
590
|
+
### complement
|
591
|
+
|
592
|
+
Given a function, returns a function that returns the negated result of the given function.
|
593
|
+
|
594
|
+
```clojure
|
595
|
+
(def not-empty? (complement empty?))
|
596
|
+
(not-empty? (list 1)) ; => true
|
597
|
+
```
|
598
|
+
|
599
|
+
|
600
|
+
### even?
|
601
|
+
|
602
|
+
Whether a number is even.
|
603
|
+
|
604
|
+
```clojure
|
605
|
+
(even? 7) ; => false
|
606
|
+
(even? 4) ; => true
|
607
|
+
```
|
608
|
+
|
609
|
+
|
610
|
+
### odd?
|
611
|
+
|
612
|
+
Whether a number is odd.
|
613
|
+
|
614
|
+
```clojure
|
615
|
+
(odd? 7) ; => true
|
616
|
+
(odd? 4) ; => false
|
617
|
+
```
|
618
|
+
|
619
|
+
|
620
|
+
### zero?
|
621
|
+
|
622
|
+
Whether a number is zero.
|
623
|
+
|
624
|
+
```clojure
|
625
|
+
(zero? 0) ; => true
|
626
|
+
(zero? 1) ; => false
|
627
|
+
```
|
628
|
+
|
629
|
+
|
630
|
+
### len
|
631
|
+
|
632
|
+
The length of a list.
|
633
|
+
|
634
|
+
```clojure
|
635
|
+
(len (list 1 2 3 4 5)) ; => 5
|
636
|
+
```
|
637
|
+
|
638
|
+
|
639
|
+
### nth
|
640
|
+
|
641
|
+
Get item in list by index.
|
642
|
+
|
643
|
+
```clojure
|
644
|
+
(nth 2 (list 0 1 2 3 4)) ; => 2
|
645
|
+
```
|
646
|
+
|
647
|
+
|
648
|
+
### last
|
649
|
+
|
650
|
+
The last item in a list.
|
651
|
+
|
652
|
+
```clojure
|
653
|
+
(last (list 0 1 2 3 4)) ; => 4
|
654
|
+
```
|
655
|
+
|
656
|
+
|
657
|
+
### reverse
|
658
|
+
|
659
|
+
Reverse the order of a list.
|
660
|
+
|
661
|
+
```clojure
|
662
|
+
(reverse (list 1 2 3)) ; => (3 2 1)
|
663
|
+
```
|
664
|
+
|
665
|
+
|
666
|
+
### map
|
667
|
+
|
668
|
+
Map a function over every item in a list.
|
669
|
+
|
670
|
+
```clojure
|
671
|
+
(map inc (list 1 2 3)) ; => (2 3 4)
|
672
|
+
```
|
673
|
+
|
674
|
+
|
675
|
+
### reduce
|
676
|
+
|
677
|
+
Perform an operation over every item in a list, carrying the result.
|
678
|
+
|
679
|
+
Parameters `(func memo coll)`:
|
680
|
+
|
681
|
+
1. A 2-arity function taking a running total and the current item `(memo item)`.
|
682
|
+
The result of this function is passed in as the `memo` the next invokation.
|
683
|
+
2. The initial value of `memo`.
|
684
|
+
3. The collection to reduce over.
|
685
|
+
|
686
|
+
Each item in the collection is passed to `func` along with the result of the
|
687
|
+
previous call.
|
688
|
+
|
689
|
+
```clojure
|
690
|
+
; Calculate the sum of a list
|
691
|
+
(reduce
|
692
|
+
(fn (memo item) (+ memo item)) ; Add the running total (memo) to this item
|
693
|
+
0 ; Start counting from zero
|
694
|
+
(list 1 2 3)) ; These will all be passed in as `item` in turn
|
695
|
+
```
|
696
|
+
|
697
|
+
Note that `+` already is a function that takes 2 parameters and adds them
|
698
|
+
together, so this example can be shortened to just this (this is in fact
|
699
|
+
exactly how the [sum](#sum) method in the standard library is implemented):
|
700
|
+
|
701
|
+
```clojure
|
702
|
+
(reduce + 0 (list 1 2 3))
|
703
|
+
```
|
704
|
+
|
705
|
+
|
706
|
+
### filter
|
707
|
+
|
708
|
+
Returns a collection with all the items that returned true when passed to the
|
709
|
+
filter-function.
|
710
|
+
|
711
|
+
```clojure
|
712
|
+
(filter odd? (list 1 2 3)) ; => (1 3)
|
713
|
+
```
|
714
|
+
|
715
|
+
|
716
|
+
### sum
|
717
|
+
|
718
|
+
The sum of all items in a list.
|
719
|
+
|
720
|
+
```clojure
|
721
|
+
(sum (list 5 10 15)) ; => 30
|
722
|
+
```
|
723
|
+
|
724
|
+
|
725
|
+
### take
|
726
|
+
|
727
|
+
Take x items from the front of a list.
|
728
|
+
|
729
|
+
```clojure
|
730
|
+
(take 2 (list 1 2 3 4)) ; => (1 2)
|
731
|
+
```
|
732
|
+
|
733
|
+
|
734
|
+
### drop
|
735
|
+
|
736
|
+
Drop x items from the front of a list.
|
737
|
+
|
738
|
+
```clojure
|
739
|
+
(drop 2 (list 1 2 3 4)) ; => (3 4)
|
740
|
+
```
|
741
|
+
|
742
|
+
|
743
|
+
### range
|
744
|
+
|
745
|
+
A range of incrementing numbers from a to b-1.
|
746
|
+
|
747
|
+
```clojure
|
748
|
+
(range 0 10) ; => (0 1 2 3 4 5 6 7 8 9)
|
749
|
+
(range 10 0) ; => ()
|
750
|
+
```
|
751
|
+
|
752
|
+
|
753
|
+
### max
|
754
|
+
|
755
|
+
The highest value in a list.
|
756
|
+
|
757
|
+
```clojure
|
758
|
+
(max (list 4 6 1 5 3)) ; => 6
|
759
|
+
```
|
760
|
+
|
761
|
+
|
762
|
+
### min
|
763
|
+
|
764
|
+
The lowest value in a list.
|
765
|
+
|
766
|
+
```clojure
|
767
|
+
(min (list 4 6 1 5 3)) ; => 1
|
768
|
+
```
|
769
|
+
|
770
|
+
### every
|
771
|
+
|
772
|
+
Take a list of every Nth item in a list.
|
773
|
+
|
774
|
+
```clojure
|
775
|
+
(every 2 (list 1 2 3 4 5)) ; => (1 3 5)
|
776
|
+
(every 3 (list 1 2 3 4 5)) ; => (1 4)
|
777
|
+
```
|
778
|
+
|
779
|
+
|
780
|
+
### text->list
|
781
|
+
|
782
|
+
Turn a text into a list of its characters.
|
783
|
+
|
784
|
+
```clojure
|
785
|
+
(text->list "abcdef") ; => ("a" "b" "c" "d" "e" "f")
|
786
|
+
```
|
787
|
+
|
788
|
+
|
789
|
+
### list->text
|
790
|
+
|
791
|
+
Turn a list into a text of its items.
|
792
|
+
|
793
|
+
```clojure
|
794
|
+
(list->text (list 1 2 3 4)) ; => "1234"
|
795
|
+
```
|
796
|
+
|
797
|
+
|
798
|
+
### ->text
|
799
|
+
|
800
|
+
Turn a value into text.
|
801
|
+
|
802
|
+
```clojure
|
803
|
+
(->text 5) ; => "5"
|
804
|
+
```
|
805
|
+
|
806
|
+
|
807
|
+
### ->integer
|
808
|
+
|
809
|
+
Turn a value into an integer.
|
810
|
+
|
811
|
+
```clojure
|
812
|
+
(->integer "5") ; => 5
|
813
|
+
(->integer 5.5) ; => 5
|
814
|
+
(->integer nil) ; => nil
|
815
|
+
```
|
816
|
+
|
817
|
+
|
818
|
+
### ->decimal
|
819
|
+
|
820
|
+
Turn a value into a decimal.
|
821
|
+
|
822
|
+
```clojure
|
823
|
+
(->decimal "5.5") ; => 5.5
|
824
|
+
(->decimal 5) ; => 5.0
|
825
|
+
(->decimal nil) ; => 0.0
|
826
|
+
```
|
827
|
+
|
828
|
+
|
829
|
+
### pipe
|
830
|
+
|
831
|
+
Pipe the first argument through a number of functions.
|
832
|
+
|
833
|
+
```clojure
|
834
|
+
(pipe 5 inc inc dec ->text) ; => "6"
|
835
|
+
```
|
836
|
+
|
837
|
+
|
838
|
+
### reverse-text
|
839
|
+
|
840
|
+
Reverse text.
|
841
|
+
|
842
|
+
```clojure
|
843
|
+
(reverse-text "hello") ; => "olleh"
|
844
|
+
```
|
845
|
+
|
846
|
+
|
847
|
+
### prompt
|
848
|
+
|
849
|
+
Request an answer to a question from a terminal user.
|
850
|
+
|
851
|
+
```clojure
|
852
|
+
(prompt "yes/no: ")
|
853
|
+
; The program halts waiting for the user, if she typed y and then pressed enter:
|
854
|
+
; => "y"
|
855
|
+
```
|
856
|
+
|
857
|
+
|
858
|
+
### defn
|
859
|
+
|
860
|
+
A shorthand macro for defining a function. As a bonus it also wraps
|
861
|
+
body-arguments in a `do`-block.
|
862
|
+
|
863
|
+
```clojure
|
864
|
+
; These are equivalent
|
865
|
+
|
866
|
+
(def plus-two
|
867
|
+
(fn (x)
|
868
|
+
(do
|
869
|
+
(println "doing stuff")
|
870
|
+
(+ x 2))))
|
871
|
+
|
872
|
+
(defn plus-two
|
873
|
+
(x)
|
874
|
+
(println "doing stuff")
|
875
|
+
(+ x 2))
|
876
|
+
```
|
877
|
+
|
878
|
+
|
879
|
+
### defm
|
880
|
+
|
881
|
+
A shorthand macro for defining a macro.
|
882
|
+
|
883
|
+
```clojure
|
884
|
+
; These are equivalent
|
885
|
+
|
886
|
+
(def infix
|
887
|
+
(macro (form)
|
888
|
+
(list (second form) (first form) (last form))))
|
889
|
+
|
890
|
+
(defm infix (form)
|
891
|
+
(list (second form) (first form) (last form)))
|
892
|
+
```
|
893
|
+
|
894
|
+
|
895
|
+
### let
|
896
|
+
|
897
|
+
Creates local variable bindings.
|
898
|
+
|
899
|
+
Parameters `(bindings & body)`:
|
900
|
+
|
901
|
+
1. Any number of bindings consisting of a symbol directly followed by a value
|
902
|
+
to bind it to. Declared in the same fashion as [dict](#dict) but with
|
903
|
+
symbols instead of text.
|
904
|
+
2. The body in which the bindings are in scope.
|
905
|
+
|
906
|
+
Here `one` is bound to `1` and `two` is bound to `2`, they are available in the
|
907
|
+
body of the `let` and then go out of scope.
|
908
|
+
|
909
|
+
```clojure
|
910
|
+
; Here we put a newline after every pair to clarify their connection:
|
911
|
+
(let (one 1
|
912
|
+
two 2)
|
913
|
+
(+ one two)) ; => 3
|
914
|
+
|
915
|
+
; The bindings are no longer in scope.
|
916
|
+
one ; !> Lasp::NameError: one is not present in this context
|
917
|
+
|
918
|
+
|
919
|
+
; Bindings can be used directly in subsequent bindings
|
920
|
+
(let (one 1
|
921
|
+
two (+ one 1))
|
922
|
+
(+ one two)) ; => 3
|
923
|
+
```
|
924
|
+
|
925
|
+
If given an uneven number of bindings, the last one will be assigned to `nil`.
|
926
|
+
|
927
|
+
```clojure
|
928
|
+
(let (one 1 two 2 three) three) ; => nil
|
929
|
+
```
|
930
|
+
|
931
|
+
|
932
|
+
### macroexpand
|
933
|
+
|
934
|
+
Returns the unevaluated result of a macro, indispensable for debugging.
|
935
|
+
|
936
|
+
```clojure
|
937
|
+
(defm infix (form)
|
938
|
+
(list (second form) (first form) (last form)))
|
939
|
+
|
940
|
+
(infix (4 + 5)) ; => 9
|
941
|
+
(macroexpand (infix (4 + 5))) ; => (+ 4 5)
|
942
|
+
```
|