15
CS 63 CS 63 LISP LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp.“ * Note: there aren’t actually nine other rules, just the tenth one.

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Embed Size (px)

Citation preview

Page 1: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

CS 63CS 63

LISPLISPPhilip Greenspun's Tenth* Rule of Programming:"Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp.“

* Note: there aren’t actually nine other rules, just the tenth one.

Page 2: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Something I stumbled across…

“…teaching Lisp by showing you how to write several complete Lisp-based games, including a text adventure, an evolution simulation, and a robot battle.”

Disclaimer: I’m not endorsing this book, I just liked the cover.

Page 3: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Why Lisp?

• Because it’s the most widely used AI programming language

• Because it’s good for writing production software (Graham article)

• Because it’s got lots of features other languages don’t

• Because you can write new programs and extend old programs really, really quickly in Lisp

Page 4: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Great! How can I get started?

• Run /usr/bin/clisp

• From http://clisp.cons.org you can download CLISP for your own PC (Windows or Linux)

• Great Lisp resource page: http://www.apl.jhu.edu/~hall/lisp.html

Page 5: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Why all those parentheses?

• Surprisingly readable if you indent properly (use built-in Lisp editor in emacs!)

• Makes prefix notation manageable• An expression is an expression is an expression,

whether it’s inside another one or not•(+ 1 2)•(* (+ 1 2) 3)•(list (* 3 5) ‘atom ‘(list inside a list) (list 3 4) ‘(((very) (very) (very) (nested list))))

Page 6: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Lisp basics

• Lisp syntax: parenthesized prefix notation

• Lisp interpreter: read-eval-print loop

• Nested evaluation

• Preventing evaluation (quote and other special forms)

• Forcing evaluation (eval)–Allows us to evaluate code contained in a Lisp

variable!

Page 7: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Basic Lisp types• Numbers (integers, floating-point, complex)–27 -2 7.519

• Characters, strings (arrays of chars)–#\x #\- #\B–“This is a string!”

• Symbols, which have property lists–‘a ‘x ‘jon

• Lists (linked cells)– Empty list: nil

–‘(a b c) ‘(2 3 jon)–cons structure has car (first) and cdr (rest)

Page 8: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Built-in functions• For numbers–+ - * / incf decf

• A diversion: destructive functions–(setf x 1)–(setf y (+ x 1)) vs. (setf y (incf x))

• For lists–car (first) cdr (rest) second third fourth

–length nth–cons append nconc list–mapcar mapcan–find remove remove-if

Page 9: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Built-in functions (cont’d)

• Printing: print, format– (print “string”) print output– (format …) formatted output

• Advanced list processing: assoc, mapcar• Predicates: listp, numberp, stringp, atom, null, equal, eql, and, or, not

• Special forms: setq/setf, quote, defun, defparameter, defconstant, if, cond, case, progn, loop

Page 10: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

More Lisp types• Arrays (with zero or more dimensions)

• Hash tables

• Streams (for reading and writing)

• Structures

• Functions, including lambda functions–(defun incBy10 (n) (+ n 10))–(mapcar #’(lambda (n) (+ n 10))

‘(1 2 3 4 5))

Page 11: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Useful help facilities•(apropos ‘str) list of symbols whose

name contains ‘str•(describe ‘symbol) description of

symbol•(describe #’fn) description of function•(trace fn) print a trace of fn as it runs•:a abort one level out of debugger

Page 12: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

A Lisp example

• Writing a function to compute the nth Fibonacci number–Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, …

• fib(0) = 0• fib(1) = 1• fib(n) = fib(n-2) + fib(n-1)

Page 13: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Complete Version(defun fib (n) (cond ((eql n 0) 0) ; base case ((eql n 1) 1) ; base case (t (+ (fib (- n 1)) ; recursively compute fib(n) (fib (- n 2))))))

Page 14: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Complete Version with Error Checking and Comments

(defun fib (n) "Computes the nth Fibonacci number." (cond ((or (not (integerp n)) (< n 0)) ; error case (error "~s must be an integer >= 0.~&" n)) ((eql n 0) 0) ; base case ((eql n 1) 1) ; base case (t (+ (fib (- n 1)) ; recursively compute fib(n) (fib (- n 2))))))

Page 15: CS 63 LISP Philip Greenspun's Tenth* Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden

Now you’ve been enlightened!…well, sort of…

Cartoon from xkcd.com