0% found this document useful (0 votes)
974 views46 pages

A Quick Introduction To Lisp

This document provides a quick introduction to the Lisp programming language. It discusses Lisp's syntax, which is expression-oriented and consists of literals, symbols, and lists. It also covers Lisp's numerical representations, variables and scoping, packages for namespacing, cons cells for linked lists, the read-eval-print loop, evaluation rules, preventing evaluation, comments, booleans, equality comparisons, functions, preventing side effects, list processing functions, recursion, I/O, assignment, iteration, blocks, conditions and restarts, function parameters, simple data types like arrays and hash tables, macros, object orientation with CLOS, multiple dispatch with generic functions, Lisp's use in real-world applications, testing with

Uploaded by

api-26004467
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
974 views46 pages

A Quick Introduction To Lisp

This document provides a quick introduction to the Lisp programming language. It discusses Lisp's syntax, which is expression-oriented and consists of literals, symbols, and lists. It also covers Lisp's numerical representations, variables and scoping, packages for namespacing, cons cells for linked lists, the read-eval-print loop, evaluation rules, preventing evaluation, comments, booleans, equality comparisons, functions, preventing side effects, list processing functions, recursion, I/O, assignment, iteration, blocks, conditions and restarts, function parameters, simple data types like arrays and hash tables, macros, object orientation with CLOS, multiple dispatch with generic functions, Lisp's use in real-world applications, testing with

Uploaded by

api-26004467
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

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: a­symbol
 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

Can have spaces in a name Can't

Can be uninterned Can't

Symbols carry data Used only for identification

Contain a property list Don't

Are candidates for GC if unbound Aren't

Lisp symbols in the keyword package are analogous to Ruby symbols


Cons Cells

 (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 (test­1 consequent­1)
      (test­n consequent­n))
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
 type­of
 boundp
 fboundp
 apropos
 documentation
λ

eval ~ apply
Side Effects
 (setf a 5)
 (random 10)
 (defvar b 10)
 (defparameter my­list '(10 20))
 (let ((var­1 val­1)
      (var­n val­n))
  (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
 find­if, remove­if, remove­if­not, 
count­if, count­if­not (filter)
Recursion
 Ideas for tail recursive functions
 (defun func (x)
  (cond (end­test end­val)
        (t (func reduced­x))))
 (labels ((fn­1 args­1 body­1)
         (fn­n args­n body­n))
  body)
IO, IO, off to work we go
 (format t ”~s to ~s~% is the time 
limit” 1 2)
 read
 yes­or­no­p, y­or­n­p
 with­open­file
Assignment
 incf, decf, push, pop
 when, unless
Iteration
 (dotimes (var n (result­form))
  body)
 (dolist (var list (result­form))
  body)
 return
 (do ((var1 init1 update1)
     (varn initn updaten))
    (test action1 actionn)
  body)
Blocks
 Function bodies are implicit blocks
 return­from
 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 my­incf (var)
  (list 'setq var (list '+ var 1)))
 (defmacro my­incf (var)
  `(setq ,var (+ 1 ,var)))
What's the difference?

Functions Macros

Arguments are always Arguments are never


evaluated evaluated

Results can be any Results can only be a


value/s valid s-expression

Results aren't evaluated Results are evaluated


To confuse you a little more
 ` , ,@
 &body
 Destructuring
 defvar, defparameter, defconstant
Object Orientation
 Older Object Systems
 Flavours: Introduced Message passing,
multiple inheritance & mixins
 New Flavours: Generic functions
 Common LOOPS
 CLOS
A CLOS Encounter
 Class, instances of classes, generic
functions and methods
 defclass, make­instance, 
defgeneric, defmethod
 Class has multiple superclasses, slots
and a meta-class
 Multiple dispatch
 Methods defined separately with no
special access
Multiple Dispatch & Generic
Functions
 Given args, a list of applicable methods is
found
 Find the most specific method
 Apply that method to the args
 Example:
 (defgeneric f (x y)) 
 (defmethod f ((x integer) y) 1) 
 (f 1 2.0) → 1
 (defmethod f ((x integer) (y real)) 2) 
 (f 1 2.0) → 2
Lisp and MacGyver

Yes, these are shamelessly stolen too


What do you mean MacGyver?
 Mid 80s to early 90s
 Prefer brain over brawn
 Used only simple tools
 Clever solutions to seemingly intractable problems*
 Entered into popular culture
 Suffered from bad marketing towards the end
 Spin-offs never as good as the original
 Some loyal old-timers still act like keepers of the flame

 * Hey! Wikipedia says so.


http://xkcd.com/224/
So what happened?
Worse is better
http://www.dreamsongs.com/WIB.html
Hmm, so who uses it now?
 ITA Software – Lisp for High-Performance
transaction processing (Cambridge, MA)
 Cleartrip (Bombay)
 Naughty Dog Software

 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

You might also like