# Common Lisp

(This first bit abridged from Paul Graham’s “ANSI Common LISP” 1996)

```> (+ 3 5)  ; + is a function
8
> (/ (- 7 1) (- 4 2))
3
> '(+ 3 5)  ; this is an abbreviation of (quote (+ 3 5))
(+ 3 5)
> 'Artichoke  ; a symbol
ARTICHOKE
> '(my 3 "Sons")  ; a list
(MY 3 "Sons")
> (list 'my (+ 2 1) "Sons")  ; calling the list function
(MY 3 "Sons")```

#### Expressions vs lists

If a list is quoted, evaluation returns the list itself; if it is not quoted, the list is treated as code, and evaluation returns its value. Here the first argument is quoted, and so yields a list. The second argument is not quoted, and is treated as a function call, yielding a number:

```> (list '(+ 2 1) (+ 2 1))
((+ 2 1) 3)
> ()  ; 1st way of representing empty list
NIL
> nil  ; 2nd way. nil evaluates to itself.
NIL
> (cons 'a '(b c d))  ; cons = build list
(A B C D)
> (car '(a b c))  ; car = extract 1st element of list
A
> (cdr '(a b c))  ; cdr = extract everything after 1st element
(B C)
> (car (cdr (cdr '(a b c d))))  ; get third element
C
> (third '(a b c d))  ; easier way
C
> (listp '(a b c))  ; listp returns true if its argument is
a list:
T```

A function whose return value is intended to be interpreted as truth or falsity is called a predicate. Common Lisp predicates often have names that end with p. Falsity in Common Lisp is represented by nil, the empty list.

```> (if (listp '(a b c))  ; if
(+ 1 2) 				; then
(+ 5 6)) 				; else
3
> (if (listp 27)  ; the 'else' argument defaults to nil.
(+ 2 3))
NIL
> (if 27 1 2)  ; everything except nil counts as logically true.
1
> (and t (+ 1 2))  ;like bash's ": && echo \$((1+2))"
3```

#### defun

Define new functions with defun. It usually takes three or more arguments: a name, a list of parameters, and one or more expressions that will make up the body of the function.

```> (defun our-third (x)
(car (cdr (cdr x))))
OUR-THIRD```

The first argument says that the name of this function will be `our-third`. The second argument, the list `(x)`, says that the function will take exactly one argument: `x`. A symbol used as a placeholder in this way is called a variable. When the variable represents an argument to a function, as `x` does, it is also called a parameter.

```> (our-third '(a b c d))
C```

Symbols are variable names, existing as objects in their own right. And that’s why symbols, like lists, have to be quoted. A list has to be quoted because otherwise it will be treated as code; a symbol has to be quoted because otherwise it will be treated as a variable.
++++You can think of a function definition as a generalized version of a Lisp expression. The following expression tests whether the sum of 1 and 4 is greater than 3:

```> (> (+ 1 4) 3)
T```

A function that will test whether the sum of any two numbers is greater than a third:

```> (defun sum-greater (x y z)
(> (+ x y) z))
SUM-GREATER
> (sum-greater 1 4 3)
T```

Lisp makes no distinction between a program, a procedure, and a function. Functions do for everything (and indeed, make up most of the language itself).
A recursive function duplicating the CL function `member`, which tests whether something is a member of itself:

```(defun our-member (obj lst)
(if (null lst)
nil
(if (eql (car lst) obj)
lst
(our-member obj (cdr lst)))))```

The predicate `eql` tests whether its two arguments are identical.

## Macros etc

#### Looping

```> (dolist (x '(1 2 3)) (print x))
1
2
3
NIL
> (dotimes (i 4) (print i))
0
1
2
3
NIL
> (dotimes (x 20) ;prints 20x20 times table
(dotimes (y 20)
(format t "~3d " (* (1+ x) (1+ y))))
(format t "~%"))```

the DO template:

```(do (variable-definition*)
(end-test-form result-form*)
statement*)```

example of do loop: (looping over n, cur and next)

```(do ((n 0 (1+ n))
(cur 0 next)
(next 1 (+ cur next)))
((= 10 n) cur))```

simple LOOP

```(loop
body-form*)```

extended LOOP

```(loop for i from 1 to 10 collecting i) ==> (1 2 3 4 5 6 7 8 9 10)
(loop for x from 1 to 10 summing (expt x 2)) ;sum the first 10 squares
==> 385```

This counts the number of vowels in a string:

```(loop for x across "the quick brown fox jumps over the lazy dog"
counting (find x "aeiou")) ==> 11```

This computes the eleventh Fibonacci number, similar to the DO loop used earlier:

```(loop for i below 10
and a = 0 then b
and b = 1 then (+ b a)
finally (return  a))```

some ‘extended LOOP’ keywords: across, and, below, collecting, counting, finally, for, from, summing, then, to
—————

```;;; True if the specified subsequence of the string is a
;;; palindrome (reads the same forwards and backwards).
(defun palindromep (string &optional
(start 0)
(end (length string)))
(dotimes (k (floor (- end start) 2) t)
(unless (char-equal (char string (+ start k))
(char string (- end k 1)))
(return nil))))```

——————————-

```;;; find the first pair of numbers, each less than 10, whose product
;;; is greater than the argument
;;; (uses RETURN-FROM to return the pair as soon as it finds it)

(defun foo (n)
(dotimes (i 10)
(dotimes (j 10)
(when (> (* i j) n)
(return-from foo (list i j))))))```

————————–

```> (defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat (funcall fn i) do (format t "*"))
(format t "~%")))
PLOT
> (plot #'exp 0 4 1/2)
*
**
***
*****
********
*************
*********************
**********************************
*******************************************************
NIL
> (plot (lambda (x) (+ 30 (* 20 (sin x)))) 0 6.4 0.3)
******************************
************************************
******************************************
**********************************************
*************************************************
**************************************************
**************************************************
************************************************
********************************************
***************************************
*********************************
***************************
**********************
*****************
*************
***********
***********
************
***************
*******************
*************************
*******************************
NIL```

[that lisp is very reminiscent of Horner’s form!
5(x+2(x+1(x+3)))=(* 5 (+ x (* 2 (+ x (* 1 (+ x 3)))))) ]
———————————————–
A LISP programmer knows the value of everything, but the cost of nothing. – Alan Perlis, Epigrams on Programming
———————
FROM THE HYPERSPEC
The backquote introduces a template of a data structure to be built. For example, writing

``(cond ((numberp ,x) ,@y) (t (print ,x) ,@y))`

is roughly equivalent to writing

```(list 'cond
(cons (list 'numberp x) y)
(list* 't (list 'print x) y))```

Where a comma occurs in the template, the expression following the comma is to be evaluated to produce an object to be inserted at that point. Assume b has the value 3, for example, then evaluating the form denoted by `(a b ,b ,(+ b 1) b) produces the result (a b 3 4 b).
If a comma is immediately followed by an @, then the form following the @ is evaluated to produce a list of objects. These objects are then “spliced” into place in the template. For example, if x has the value (a b c), then

``` `(x ,x ,@x foo ,(cadr x) bar ,(cdr x) baz ,@(cdr x))
=>  (x (a b c) a b c foo b bar (b c) baz b c)```

#### Recommended

Practical Common Lisp – A great book. Caution: Trying to obtain the Lisp package he recommends as a painlessly easy way to get started is NOT RECOMMENDED unless you like wasting time and frustration.
Sayings of John McCarthy