| Rev | Author | # | Line |
|---|---|---|---|
| 2 | GlynWebster | 1 | !!! [LISP]’s very basics |
| 2 | |||
| 3 | ''In the examples, any bold stuff is the result of running the code above it.'' | ||
| 1 | GlynWebster | 4 | |
| 5 | This is a list: | ||
| 7 | AristotlePagaltzis | 6 | |
| 7 | <verbatim> | ||
| 8 | (apple orange banana) | ||
| 9 | </verbatim> | ||
| 1 | GlynWebster | 10 | |
| 11 | Lists can be nested: | ||
| 7 | AristotlePagaltzis | 12 | |
| 13 | <verbatim> | ||
| 14 | ((fruit (apple orange banana)) (colour (red green))) | ||
| 15 | </verbatim> | ||
| 1 | GlynWebster | 16 | |
| 17 | Lisp usually interprets lists as function calls: | ||
| 7 | AristotlePagaltzis | 18 | |
| 19 | <pre> | ||
| 20 | (+ 40 2) | ||
| 21 | <b>42</b> | ||
| 22 | </pre> | ||
| 1 | GlynWebster | 23 | |
| 24 | Lisp usually interprets symbols (i.e. identifiers) as variable references: | ||
| 25 | |||
| 7 | AristotlePagaltzis | 26 | <pre> |
| 27 | (* pi 2) | ||
| 28 | <b>6.2831853071795862</b> | ||
| 29 | </pre> | ||
| 30 | |||
| 31 | A single-quote character "quotes" something, i.e. prevents Lisp from interpreting it. So the following is a list of symbols: | ||
| 32 | |||
| 33 | <verbatim> | ||
| 34 | '(elem1 elem2 elem3) | ||
| 35 | </verbatim> | ||
| 36 | |||
| 37 | <tt>let</tt> creates a list of variables that can be used in an expression: | ||
| 38 | |||
| 39 | <verbatim> | ||
| 40 | (let ((a 1) (b 2) (mylist '(a b))) expression) | ||
| 41 | </verbatim> | ||
| 42 | |||
| 43 | Hereafter, <tt>a</tt> will be 1, <tt>b</tt> = 2, <tt>mylist</tt> will contain the list <tt>'(a b)</tt>. | ||
| 44 | |||
| 45 | <tt>setq</tt> changes the value of a variable: | ||
| 46 | |||
| 47 | <verbatim> | ||
| 48 | (setq mylist '(a b c)) | ||
| 49 | </verbatim> | ||
| 50 | |||
| 51 | The <tt>cons</tt> function makes a pair: | ||
| 52 | |||
| 53 | <verbatim> | ||
| 54 | (cons 'first 42) | ||
| 55 | </verbatim> | ||
| 56 | |||
| 57 | Cons pairs are usually used to make lists: the first element is the first element on the list; the second element is either a cons pair for the rest of the list or a <tt>nil</tt> to mark the end of the list. Consider the following list: | ||
| 58 | |||
| 59 | <verbatim> | ||
| 60 | '(1 2 3) | ||
| 61 | </verbatim> | ||
| 62 | |||
| 63 | This list is a shorthand for: | ||
| 64 | |||
| 65 | <verbatim> | ||
| 66 | (cons 1 (cons 2 (cons 3 nil))) | ||
| 67 | </verbatim> | ||
| 68 | |||
| 69 | <tt>car</tt> returns the first element of a list: | ||
| 70 | |||
| 71 | <pre> | ||
| 72 | (print (car mylist)) | ||
| 73 | <b>a</b> | ||
| 74 | </pre> | ||
| 75 | |||
| 76 | <tt>cdr</tt> returns the rest of the list (ie all-but-first): | ||
| 77 | |||
| 78 | <pre> | ||
| 79 | (print (cdr mylist)) | ||
| 80 | <b>(b c)</b> | ||
| 81 | </pre> | ||
| 1 | GlynWebster | 82 | |
| 7 | AristotlePagaltzis | 83 | And that is basically [LISP]. |
| 1 | GlynWebster | 84 | |
| 7 | AristotlePagaltzis | 85 | !!! A somewhat realistic example |
| 1 | GlynWebster | 86 | |
| 7 | AristotlePagaltzis | 87 | The following example is an inefficient factorial function: |
| 1 | GlynWebster | 88 | |
| 7 | AristotlePagaltzis | 89 | <verbatim> |
| 90 | (defun fact (x) ;a recursive function | ||
| 91 | (if (> x 0) | ||
| 92 | (* x (fact (- x 1))) | ||
| 93 | 1)) | ||
| 94 | </verbatim> | ||
| 1 | GlynWebster | 95 | |
| 7 | AristotlePagaltzis | 96 | (This stupid function has become the HelloWorld of functional ProgrammingLanguage~s for some reason.) |
| 1 | GlynWebster | 97 | |
| 2 | GlynWebster | 98 | !!! Further reading |
| 99 | |||
| 100 | Look for the tutorial links on the [LISP] page. | ||
| 1 | GlynWebster | 101 | |
| 7 | AristotlePagaltzis | 102 | !!! Aside: where do these weird names come from? |
| 1 | GlynWebster | 103 | |
| 7 | AristotlePagaltzis | 104 | The names "<tt>car</tt>" and "<tt>cdr</tt>" came from the first machine that Lisp was written for: there were 2 registers and the commands "<tt>contents of address register</tt>" and "<tt>contents of decrement register</tt>". CommonLisp allows the more sensible names "<tt>first</tt>" and "<tt>rest</tt>". |
lib/blame.php:177: Warning: Invalid argument supplied for foreach() (...repeated 2 times)