A Quick Introduction To Lisp
A Quick Introduction To Lisp
M. Habibullah Pagarkar
http://xkcd.com/297/
Syntax
Expression oriented language
Consists of many expressions or forms
3 Kinds of expressions
Literals: 0.42e2, ”hello”, #\c, #(1 2)
Symbols: asymbol
Lists: (f x y)
Numerical Nonsense
Number
Real
Rational
Integer
Fixnum
Bignum
Ratio
Float
Short-float
Single-float
Double-float
Long-float
Complex
Variables
Lexical scope
Variables have no types. Objects do
Symbols are evaluated as variables
Bound
Unbound
A symbol differs from it's printed
representation
'ooga and 'ooga\ booga
Interning
Packages
Namespacing
Symbol '*package*' stores the current
package
Exporting symbols
External symbols → package:name
Internal symbols → package::name
Keyword package
So these are like Ruby's
symbols, right? Wrong.
Common Lisp symbols Ruby symbols
Unique in a package Globally unique
(cons 1 2) → (1 . 2)
(cons 1 '()) → '(1)
From wikipedia
Read-Eval-Print-Loop
Read form input → Lisp object
Eval input object → output object
Print output object using some sort of
print representation
Loop
Evaluation
Literals are self evaluating
Symbols are looked up in lexical scope
Lists
Function name as first element
All subsequent symbols are evaluated
Function called with these params
Eager evaluation is default behaviour
Preventing Evaluation
Special form (quote expression)
Abbreviated as '(expression)
Comments
;;;
;;
;
#|
Block comment
|#
Booleans
t and nil are constants
(or exp1 exp2)
(and exp1 exp2)
(if test exp1 exp2)
(cond (test1 consequent1)
(testn consequentn))
Equality Excesses
eq → Symbols
eql → Numbers of the same type - default
equal → Lists
= → Best for numbers
equalp → Equality predicate
string-equals → Strings
Regular and Special Functions
(function hello)
Or simply #'hello
(defun hello ()
”Do not mind me. I am a string.”
(print ”hello”))
I wanna know more!
describe
typeof
boundp
fboundp
apropos
documentation
λ
eval ~ apply
Side Effects
(setf a 5)
(random 10)
(defvar b 10)
(defparameter mylist '(10 20))
(let ((var1 val1)
(varn valn))
(body))
let*
LISt Processing
'() → nil
(cons 1 (cons 2 (cons 3 nil))) →
(list 1 2 3) →
'(1 2 3)
listp, endp, first, second, tenth,
rest, car, cdr, cadr, cdddr,
member, intersection, nth,
nthcdr, append, butlast, reverse,
remove etc
List Processing
assoc, rassoc
subst
sublis
Destructive List Surgery
nconc, nsubst, nreverse, nunion,
nintersection, delete
Applicative Programming
funcall
map and it's brethren
lambda
reduce, every
findif, removeif, removeifnot,
countif, countifnot (filter)
Recursion
Ideas for tail recursive functions
(defun func (x)
(cond (endtest endval)
(t (func reducedx))))
(labels ((fn1 args1 body1)
(fnn argsn bodyn))
body)
IO, IO, off to work we go
(format t ”~s to ~s~% is the time
limit” 1 2)
read
yesornop, yornp
withopenfile
Assignment
incf, decf, push, pop
when, unless
Iteration
(dotimes (var n (resultform))
body)
(dolist (var list (resultform))
body)
return
(do ((var1 init1 update1)
(varn initn updaten))
(test action1 actionn)
body)
Blocks
Function bodies are implicit blocks
returnfrom
block
prog1, prog2, progn
Conditions and Restarts
break, error, warn, cerror
Function Parameters
(defun foo (x &optional (y 2) (z
5) ())
&rest
&key
&aux
Simple Objects
Arrays
Property Lists
Structures
Hash Tables
Macros
Way to extend Lisp's syntax
Express concepts concisely
Expanded at compile time
It creates an expression that Lisp evals
(defmacro myincf (var)
(list 'setq var (list '+ var 1)))
(defmacro myincf (var)
`(setq ,var (+ 1 ,var)))
What's the difference?
Functions Macros
Stumpwm
Movitz
Oh and this little OS called Emacs
What about TDD?
LispUnit
FiveAM
Stefil
LIFT
cl-unit
and others
Books on Common Lisp
ANSI Common Lisp - Paul Graham
Practical Common Lisp - Peter Seibel
On Lisp - Paul Graham
Paradigms of AI Programming – Peter
Norvig
Lisp – Winston and Horn
Books on Scheme
The Little Schemer series – Daniel
Friedman and Matthias Felleisen
SICP - Gerald Sussman and Harold
Abelson
The Scheme Programming Language –
Kent Dybvig
Teach Yourself Scheme in Fixnum Days –
Dorai Sitaram
Upcoming Books
Land of Lisp – Conrad Barski (2010)
www.lisperati.com
Lisp Outside the Box – Nick Levine (2010)
Which Lisp?
Linux → SBCL*
Mac OS X → Clozure CL*
Windows → Corman CL, Allegro CL,
LispWorks, CLISP*
Or try Scheme/Clojure
Try to accept Emacs :)
http://gigamonkeys.com/lispbox/
http://common-lisp.net/~dlw/LispSurvey.html
* Open Source