Title: Common Lisp
1Common Lisp
2General differences from Scheme
- CL is a much bigger language than Scheme. It has
- packages (to organize namespace)
- types (optional, for efficiency only)
- structures, arrays, sequences, hash tables
(Scheme has vectors) - complex arithmetic (option in Scheme)
- powerful iteration construct (loop)
- exception handling
- fancy file I/O
- Full object-oriented extension Common Lisp
Object Standard (CLOS)
3Binding-related differences
- A variable has two slots
- value slot (set by setq (like set!))
- function slot (set by defun (like define))
- Extra syntax required to
- To create a closure (function special form).
- To execute the closure stored in the value slot
of a variable (funcall, apply special form). - To access the function stored in the function
slot of a variable (symbol-function form) - For local function definitions (flet, labels
special form) - Empty list doubles up as false.
- Static (lexical) and dynamic (special) variables.
4Declarations
- Declarations (except special) are optional
- Declarations used to
- perform extra error checking
- produce more efficient code
- Special declarations
- variable bindings are dynamic
- not pervasive (inner bindings shadow a special
declaration, must be redeclared special) - pervasive available (with defvar)
5Example of defun/function/funcall
(defun adder (x) (function (lambda (y) ( x
y)))) (setq add3 (adder 3)) (funcall add3 5)
gt 8 (defun twice (n) ( n 2)) gt TWICE
(symbol-function 'twice) gt ltFUNCTION
TWICEgt (list (twice 3) (funcall (function
twice) 3) (funcall (symbol-function
'twice) 3)) gt (6 6 6)
6Example of local functions
(defun recursive-times (k n) (labels ((temp
(n) (if (zerop n)
0 ( k (temp (1-
n)))))) (temp n))) gt RECURSIVE-TIMES
(recursive-times 2 3) gt 6
7Example of local functions
(defun integer-power (n k) A highly "bummed"
integer (declare (integer n)) exponentiation
routine (declare (type (integer 0 ) k))
(labels ((expt0 (x k a) (declare
(integer x a) (type (integer 0 ) k))
(cond ((zerop k) a)
((evenp k) (expt1 ( x x) (floor k 2) a))
(t (expt0 ( x x) (floor k 2) ( x
a))))) (expt1 (x k a)
(declare (integer x a) (type (integer 1 ) k))
(cond ((evenp k) (expt1 ( x x)
(floor k 2) a)) (t (expt0 (
x x) (floor k 2) ( x a)))))) (expt0 n k
1)))
8Example of special variables
(defun hack (thing mod) (declare (special
mod)) binding of mod visible to hack1
(hack1 (car thing))) but not that of
thing (defun hack1 (arg) (declare (special
mod)) refs to mod within hack1 are
special (if (atom arg) mod (cons
(hack1 (car arg)) (hack1 (cdr
arg)))))
9How to play with Common Lisp
- To run the Common Lisp interpreter on borg,
typeeem/software/clisp/bin/clisp - Put your Common Lisp function definitions in
file.lisp in the current directory (if you use
emacs, you get some nifty features) - In CLisp, type (load file.lisp)
- In CLisp, type CLisp expressions for evaluation.
- Best to run CLisp from within emacs(ESC-x 2,
ESC-x shell splits your window and gets you a
shell within emacs in one window, in which you
can run CLisp.Edit file.lisp in the other
window). - Consult the on-line CLisp documentation and the
CLisp reference manual. - Visit the CLisp links from the course web page.