articles/lisp.md: update
This commit is contained in:
parent
87a1bfb133
commit
07b6113414
1 changed files with 25 additions and 24 deletions
|
@ -18,51 +18,52 @@ enlightenment.
|
|||
A list in Lisp is composed of `cons` cells which have two fields: the contained value of the cell known as `car` and a pointer to the
|
||||
next cell known as `cdr`. To mark the end of a list, a special sentinel value, `NIL` is used, which represents an empty list.
|
||||
|
||||
Here's cons cell:
|
||||
```
|
||||
A cons cell:
|
||||
|
||||
+---+---+
|
||||
| O | O-|----> cdr
|
||||
+---+---+
|
||||
|
|
||||
`'´
|
||||
car
|
||||
```
|
||||
|
||||
To create a cell, `cons` is used:
|
||||
(cons 1 2) ; 1 and 2 are the car and cdr respectively
|
||||
(car (cons 1 2)) ; ==> 1
|
||||
(cdr (cons 1 2)) ; ==> 2
|
||||
|
||||
Also some Lisps accept the "dotted pair" notation as a shorthand:
|
||||
(1 . 2)
|
||||
|
||||
|
||||
And a list, composed of linked cons cells where `NIL` is the end sentinel:
|
||||
To create a cell, the builtin function `cons` is used:
|
||||
```lisp
|
||||
(cons 1 2) ; 1 and 2 are the car and cdr respectively
|
||||
(car (cons 1 2)) ; ==> 1
|
||||
(cdr (cons 1 2)) ; ==> 2
|
||||
;; Also some Lisps accept the "dotted pair" notation as a shorthand:
|
||||
(1 . 2)
|
||||
```
|
||||
|
||||
And here's a list:
|
||||
```
|
||||
cdr cdr cdr cdr
|
||||
+---+---+ +---+---+ +---+---+ +---+---+
|
||||
| O | O-|---->| O | O-|---->| O | O-|---->| o | O-|----> NIL
|
||||
+-|-+---+ +---+---+ +---+---+ +---+---+
|
||||
car | car | car | car |
|
||||
`'´ `'´ `'´ `'´
|
||||
20 "Lisp" (1 2) 39
|
||||
20 "Lisp" (1 2) 39
|
||||
```
|
||||
|
||||
Using cons:
|
||||
(cons 20 (cons "Lisp" (cons '(1 2) (cons 39 '()))))
|
||||
|
||||
In cons dotted pair notation:
|
||||
(20 . ("Lisp" . ('(1 2) . (39 . NIL))))
|
||||
|
||||
Using quote:
|
||||
'(20 "Lisp" '(1 2) 39)
|
||||
Creating lists:
|
||||
```lisp
|
||||
;; Using cons:
|
||||
(cons 20 (cons "Lisp" (cons '(1 2) (cons 39 '()))))
|
||||
;; In cons dotted pair notation:
|
||||
(20 . ("Lisp" . ('(1 2) . (39 . NIL))))
|
||||
;; Using quote:
|
||||
'(20 "Lisp" '(1 2) 39)
|
||||
;; Most Lisps provide a `list` function:
|
||||
(list 20 "Lisp" '(1 2) 39)
|
||||
```
|
||||
|
||||
## Lisp languages in use
|
||||
- [Scheme](scheme.md): created by the hackers Guy Steele and Gerald Sussman in 1975 at the MIT. Has multiple implementations, Chez Scheme,
|
||||
Chicken Scheme, MIT/GNU Scheme, etc.
|
||||
- [Scheme](scheme.md): created by the hackers Guy Steele and Gerald Sussman in 1975 at the MIT.
|
||||
- [Common Lisp](common_lisp.md): Big for Lisp standards (still better and smaller than "modern" languages), is also the most used Lisp today.
|
||||
The main implementation is SBCL, a public domain JIT compiler.
|
||||
- GNU Guille
|
||||
- Clojure: based in the [JVM](jvm.md) (Java Virtual Machine), does not have `cons`, `car` or `cdr`.
|
||||
- ...
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue