You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/conditionals/about.md
+14-16
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,8 @@
2
2
3
3
Common Lisp provides the programmer with several different conditionals that can be categorised by the number of "branches" they support.
4
4
5
-
Also, unlike many other programming languages, all conditionals in Common Lisp are _expressions_ not statements. This means that all Lisp conditionals evaluate to some value and can be substituted for concrete parameters.
5
+
Also, unlike many other programming languages, all conditionals in Common Lisp are _expressions_ not statements.
6
+
This means that all Lisp conditionals evaluate to some value and can be substituted for concrete parameters.
6
7
7
8
As an example:
8
9
@@ -30,7 +31,9 @@ The `if` conditional evaluates the first expression of the body when the test is
30
31
31
32
## Many-Branch Conditionals
32
33
33
-
The Lisp "super-conditional" is `cond`, which can have an infinite number of branches. Each branch has a test condition and body expression that are surrounded by an extra pair of parentheses. If all of the tests evaluate to false, then `nil` is returned.
34
+
The Lisp "super-conditional" is `cond`, which can have an infinite number of branches.
35
+
Each branch has a test condition and body expression that are surrounded by an extra pair of parentheses.
36
+
If all of the tests evaluate to false, then `nil` is returned.
34
37
35
38
```lisp
36
39
(cond ((= 0 2) 'nope)
@@ -41,7 +44,9 @@ The Lisp "super-conditional" is `cond`, which can have an infinite number of bra
41
44
; => QUITE-TRUE
42
45
```
43
46
44
-
If you just want to test one value against a number of branches, you can use the cleaner `case` expression. If none of the cases match, `nil` is returned. Both `t` and `otherwise` can be used as catch-all cases
47
+
If you just want to test one value against a number of branches, you can use the cleaner `case` expression.
48
+
If none of the cases match, `nil` is returned.
49
+
Both `t` and `otherwise` can be used as catch-all cases:
45
50
46
51
```lisp
47
52
(case 'elder-beast
@@ -54,20 +59,16 @@ If you just want to test one value against a number of branches, you can use the
54
59
55
60
## The Stealth Conditionals
56
61
57
-
The boolean `and` and `or` operations in Common Lisp are short-circuiting macros
58
-
that can be used to reduce the duplication of certain conditional
59
-
expressions.
62
+
The boolean `and` and `or` operations in Common Lisp are short-circuiting macros that can be used to reduce the duplication of certain conditional expressions.
60
63
61
-
The `and` macro will immediately return `nil` if a single false value is
62
-
encountered, but the last true value of the `and` otherwise:
64
+
The `and` macro will immediately return `nil` if a single false value is encountered, but the last true value of the `and` otherwise:
63
65
64
66
```lisp
65
67
(and 42 "Magic" :cool) ; => :COOL
66
68
(and () "Magic" :cool) ; => NIL
67
69
```
68
70
69
-
The `or` macro returns the first true value it encounters or `nil` if there were
70
-
no true values:
71
+
The `or` macro returns the first true value it encounters or `nil` if there were no true values:
71
72
72
73
```lisp
73
74
(or () 42 nil) ; => 42
@@ -76,13 +77,10 @@ no true values:
76
77
77
78
## I'm Exhausted...
78
79
79
-
As mentioned previously, when none of the branches in a `case` statement match,
80
-
and there is no `otherwise` clause, `nil` is returned. Occasionally, however, a
81
-
failure to match any branch should be treated as an error – this is where
82
-
`ecase` (for **exhaustive** matching) comes in.
80
+
As mentioned previously, when none of the branches in a `case` statement match, and there is no `otherwise` clause, `nil` is returned.
81
+
Occasionally, however, a failure to match any branch should be treated as an error – this is where `ecase` (for **exhaustive** matching) comes in.
83
82
84
-
It's used in exactly the same way as `case`, but signals an error instead of
85
-
returning `nil` when there is no match.
83
+
It's used in exactly the same way as `case`, but signals an error instead of returning `nil` when there is no match.
Copy file name to clipboardExpand all lines: concepts/default-parameters/about.md
+6-3
Original file line number
Diff line number
Diff line change
@@ -1,22 +1,25 @@
1
1
# About
2
2
3
-
An optional parameter is designated by the `&optional` lambda list keyword in a lambda list. Optional parameters are not required, can have a default and also can specify a "supplied-p parameter" which will be "true" or "false" depending on whether an argument was provided for the parameter.
3
+
An optional parameter is designated by the `&optional` lambda list keyword in a lambda list.
4
+
Optional parameters are not required, can have a default and also can specify a "supplied-p parameter" which will be "true" or "false" depending on whether an argument was provided for the parameter.
Optional parameters must be put after required parameters but before named or rest parameters. Arguments are first bound to required parameters, then optional parameters and finally to rest and named parameters.
12
+
Optional parameters must be put after required parameters but before named or rest parameters.
13
+
Arguments are first bound to required parameters, then optional parameters and finally to rest and named parameters.
While multiple types of parameters can be combined with other types of parameters (optional and keyword arguments) this can be be problematic and should be done carefully. See the section on ("Mixing Different Parameter Types")(pcl-function) in Practical Common Lisp.
21
+
While multiple types of parameters can be combined with other types of parameters (optional and keyword arguments) this can be be problematic and should be done carefully.
22
+
See the section on ("Mixing Different Parameter Types")(pcl-function) in Practical Common Lisp.
Copy file name to clipboardExpand all lines: concepts/default-parameters/introduction.md
+6-1
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,11 @@
1
1
# Introduction
2
2
3
-
In Common Lisp a function can have some arguments are are optional. These are designated in the lambda list by `&optional` lambda list keyword. A parameter will be bound to the value `nil` if it is not specified. If there are several optional parameters they are bound in order. Default values can be specified for optional parameters. Finally a symbol an be specified for each optional parameter which will be bound to true or false depending on whether that parameter was supplied by the caller of the function (this is referred to as the "supplied-p parameter").
3
+
In Common Lisp a function can have some arguments are are optional.
4
+
These are designated in the lambda list by `&optional` lambda list keyword.
5
+
A parameter will be bound to the value `nil` if it is not specified.
6
+
If there are several optional parameters they are bound in order.
7
+
Default values can be specified for optional parameters.
8
+
Finally a symbol an be specified for each optional parameter which will be bound to true or false depending on whether that parameter was supplied by the caller of the function (this is referred to as the "supplied-p parameter").
4
9
5
10
```lisp
6
11
(defun default-parameters (&optional x (y 'default) (z nil z-supplied-p))
parameters](../rest-parameters/about.md). These will be discussed in
25
-
later concepts.
17
+
Functions can also take [optional parameters](../default-parameters/about.md), [keyword parameters](../named-parameters/about.md), [rest parameters](../rest-parameters/about.md).
18
+
These will be discussed in later concepts.
26
19
27
-
The name of the function is in [scope](../scope/about.md) in the body of
28
-
the function so that [recursion](../recursion/about.md) can be
29
-
performed.
20
+
The name of the function is in [scope](../scope/about.md) in the body of the function so that [recursion](../recursion/about.md) can be performed.
Copy file name to clipboardExpand all lines: concepts/functions/introduction.md
+8-16
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,8 @@
1
1
# Introduction
2
2
3
-
To define a global function in Common Lisp one uses the `defun`
4
-
expression. This expression takes as its first argument a list of
5
-
parameters (and empty list means the function has no parameters). This
6
-
is followed by an optional documentation string (see below), then zero
7
-
or more expressions which make up the "body" of the function.
3
+
To define a global function in Common Lisp one uses the `defun` expression.
4
+
This expression takes as its first argument a list of parameters (and empty list means the function has no parameters).
5
+
This is followed by an optional documentation string (see below), then zero or more expressions which make up the "body" of the function.
8
6
9
7
Functions may have zero or more parameters.
10
8
@@ -16,23 +14,17 @@ Functions may have zero or more parameters.
16
14
(defun add-nums (x y) (+ x y))
17
15
```
18
16
19
-
Calling a function is done by evaluating an expression with the symbol
20
-
designating the function as the first element of the expression with
21
-
the arguments to the function (if any) as the remaining items in the
22
-
expression.
17
+
Calling a function is done by evaluating an expression with the symbol designating the function as the first element of the expression with the arguments to the function (if any) as the remaining items in the expression.
23
18
24
-
The value that a function evaluates to is the value of the last
25
-
expression in the function body that was evaluated. All functions
26
-
evaluate to a value.
19
+
The value that a function evaluates to is the value of the last expression in the function body that was evaluated. All functions evaluate to a value.
27
20
28
21
```lisp
29
22
(add-nums 2 2) ;; => 4
30
23
```
31
24
32
-
Functions can also have, optionally, a documentation string (also
33
-
called a 'docstring'). If provided it comes after the argument list
34
-
but before the body of the function. The documentation string can be
35
-
accessed via `documentation`.
25
+
Functions can also have, optionally, a documentation string (also called a 'docstring').
26
+
If provided it comes after the argument list but before the body of the function.
27
+
The documentation string can be via `documentation`.
36
28
37
29
```lisp
38
30
(defun add-nums (x y) "Add X and Y together" (+ x y))
Copy file name to clipboardExpand all lines: concepts/lambda-list/about.md
+4-2
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,12 @@
1
1
# About
2
2
3
-
In Common Lisp a parameter list (such as for defining a function) is also known as a (lambda list)[lambda-list]. Lambda lists are also used for defining macros and destructuring.
3
+
In Common Lisp a parameter list (such as for defining a function) is also known as a (lambda list)[lambda-list].
4
+
Lambda lists are also used for defining macros and destructuring.
4
5
5
6
Lambda lists can contain (lambda list keywords)[lambda-list-keyword] such as `&rest`, `&optional`, `&key` and others.
6
7
7
-
While multiple types of parameters can be combined with other types of parameters (optional and keyword arguments) this can be be problematic and should be done carefully. See the section on ("Mixing Different Parameter Types")(pcl-function) in Practical Common Lisp.
8
+
While multiple types of parameters can be combined with other types of parameters (optional and keyword arguments) this can be be problematic and should be done carefully.
9
+
See the section on ("Mixing Different Parameter Types")(pcl-function) in Practical Common Lisp.
Copy file name to clipboardExpand all lines: concepts/lambda-list/introduction.md
+6-1
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,11 @@
1
1
# Introduction
2
2
3
-
In Common Lisp a function's argument list is known as a ('lambda list')[lambda-list]. A lambda list can can have arguments of different types. These different types are designated with the use of ('lambda list keywords')[lambda-list-keyword] which all begin with `&`. The most commonly used types are optional, keyword and rest arguments types. Every parameter in the lambda list after a particular lambda list keyword is will be of that type. A lambda list keyword can only be used once in a lambda list.
3
+
In Common Lisp a function's argument list is known as a ('lambda list')[lambda-list].
4
+
A lambda list can can have arguments of different types.
5
+
These different types are designated with the use of ('lambda list keywords')[lambda-list-keyword] which all begin with `&`.
6
+
The most commonly used types are optional, keyword and rest arguments types.
7
+
Every parameter in the lambda list after a particular lambda list keyword is will be of that type.
8
+
A lambda list keyword can only be used once in a lambda list.
4
9
5
10
Lambda lists are also used in other constructs which will be discussed later such as destructuring and macros.
[Lists][hyper-cons-as-list] are a very common data type in Common Lisp. They are made up of a sequence of [cons][../cons/about.md] cells. Each `car` is an element of the list and every `cdr` is a either the next cons cell or a terminating atom.
3
+
[Lists][hyper-cons-as-list] are a very common data type in Common Lisp.
4
+
They are made up of a sequence of [cons][../cons/about.md] cells.
5
+
Each `car` is an element of the list and every `cdr` is a either the next cons cell or a terminating atom.
4
6
5
7
A list which terminates with the empty list is called a "proper list".
6
8
7
9
A list which terminates with an atom that is not th empty list is called a "dotted list" (based upon how it is printed: `(cons 'a 'b) ;=> (a . b)`).
8
10
9
-
A list can also be circular if some cons cell in the list `cdr` of a later cons cell. For example: `(let ((x (list 1))) (setf (cdr x) x) (write x :stream t :circle t) :done)`. Take care when working with circular lists as allowing them to be printed out without binding [`*print-circle*`][hyper-print-circle] to `t` will cause an infinite loop and will lock up the REPL (in this example by specifying `:circle t` to `write`, `*print-circle` will be bound to `t`).
11
+
A list can also be circular if some cons cell in the list `cdr` of a later cons cell.
12
+
For example: `(let ((x (list 1))) (setf (cdr x) x) (write x :stream t :circle t) :done)`.
13
+
Take care when working with circular lists as allowing them to be printed out without binding [`*print-circle*`][hyper-print-circle] to `t` will cause an infinite loop and will lock up the REPL (in this example by specifying `:circle t` to `write`, `*print-circle` will be bound to `t`).
Copy file name to clipboardExpand all lines: concepts/lists/introduction.md
+7-3
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,9 @@ Given that the name of the language is Lisp which stands of _LISt Processing_ on
4
4
5
5
While Common Lisp has other data structures as well as lists, lists are still heavily used.
6
6
7
-
A list in Common Lisp is a sequence of items. The items themselves do not have to be the same type. For example you can have a list of `1`, `two`, `"III"`.
7
+
A list in Common Lisp is a sequence of items.
8
+
The items themselves do not have to be the same type.
9
+
For example you can have a list of `1`, `two`, `"III"`.
8
10
9
11
## Creating Lists
10
12
@@ -32,11 +34,13 @@ There are also two main functions used to create lists: `list` and `cons`.
32
34
33
35
## Length & Random Access
34
36
35
-
The length of a list can be determined by the use of `length`. An empty list has length zero.
37
+
The length of a list can be determined by the use of `length`.
38
+
An empty list has length zero.
36
39
37
40
An arbitrary item can be accessed with `nth` (note that lists are zero-indexed).
38
41
39
-
It is _not_ an error to request an index that is more than the length. Instead it evaluates to `nil`:
42
+
It is _not_ an error to request an index that is more than the length.
Copy file name to clipboardExpand all lines: concepts/named-parameters/about.md
+6-3
Original file line number
Diff line number
Diff line change
@@ -2,22 +2,25 @@
2
2
3
3
In Common Lisp named parameters are called keyword parameters.
4
4
5
-
Keyword parameters are designated by the `&key` lambda list keyword in a lambda list. Keyword parameters are not required, can have a default and also can specify a "supplied-p parameter" which will be "true" or "false" depending on whether an argument was provided for the parameter.
5
+
Keyword parameters are designated by the `&key` lambda list keyword in a lambda list.
6
+
Keyword parameters are not required, can have a default and also can specify a "supplied-p parameter" which will be "true" or "false" depending on whether an argument was provided for the parameter.
6
7
7
8
```lisp
8
9
(defun keyword-parameter (&key (arg -1) arg)
9
10
(keyword-parameter) ;; => -1
10
11
(keyword-parameter :arg 13) ;; => 13
11
12
```
12
13
13
-
In the arguments to a function the keyword parameters are specified by their "keyword name" which is, by default, a keyword symbol version of the parameter name (_i.e._ keyword parameter `name` has a keyword name of `:name`). It is possible to specify another name for the keyword parameter by using a list of keyword name and parameter name instead of just the parameter name:
14
+
In the arguments to a function the keyword parameters are specified by their "keyword name" which is, by default, a keyword symbol version of the parameter name (_i.e._ keyword parameter `name` has a keyword name of `:name`).
15
+
It is possible to specify another name for the keyword parameter by using a list of keyword name and parameter name instead of just the parameter name:
While multiple types of parameters can be combined with other types of parameters (optional and keyword arguments) this can be be problematic and should be done carefully. See the section on ("Mixing Different Parameter Types")(pcl-function) in Practical Common Lisp.
22
+
While multiple types of parameters can be combined with other types of parameters (optional and keyword arguments) this can be be problematic and should be done carefully.
23
+
See the section on ("Mixing Different Parameter Types")(pcl-function) in Practical Common Lisp.
Copy file name to clipboardExpand all lines: concepts/named-parameters/introduction.md
+6-2
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,12 @@
1
1
# Introduction
2
2
3
-
In Common Lisp a function can have named parameters (referred to as "keyword parameters" or "keyword arguments"). These are designated in the lambda list by the `&key` lambda list keyword. Keyword parameters are not required parameters. Like optional parameters they can be given default values and symbols to bind to their 'supplied-or-not' state.
3
+
In Common Lisp a function can have named parameters (referred to as "keyword parameters" or "keyword arguments").
4
+
These are designated in the lambda list by the `&key` lambda list keyword.
5
+
Keyword parameters are not required parameters.
6
+
Like optional parameters they can be given default values and symbols to bind to their 'supplied-or-not' state.
4
7
5
-
When calling a function with keyword parameters the name of the parameter as a keyword is used in front of the parameter value. Keyword parameters can be specified by the caller of the function in any order.
8
+
When calling a function with keyword parameters the name of the parameter as a keyword is used in front of the parameter value.
9
+
Keyword parameters can be specified by the caller of the function in any order.
6
10
7
11
```lisp
8
12
(defun keyword-parameters (&key x (y 'default) (z nil z-supplied-p))
0 commit comments