CMSC 471 LISP - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CMSC 471 LISP

Description:

Special forms: setq/setf, quote, defun, defparameter, defconstant, if, cond, case, progn, loop ... (invert-aux (cdr mylist) (cons (car mylist) outlist) ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 15
Provided by: erice8
Category:
Tags: cmsc | lisp

less

Transcript and Presenter's Notes

Title: CMSC 471 LISP


1
CMSC 471LISP
2
Comic from xkcd.com
3
Why Lisp?
  • Because its good for writing production software
  • Because its got lots of features other languages
    dont
  • Because you can write new programs and extend old
    programs really, really quickly in Lisp

4
Great! How can I get started?
  • On sunserver (CS) and gl machines, run
    /usr/local/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

5
Basic Lisp
  • Surprisingly readable if you indent properly
    (dont use a plain text editor)
  • Lisp syntax parenthesized prefix notation
  • Lisp interpreter read-eval-print loop
  • ( 1 2)
  • ( ( 1 2) 3)
  • (list ( 3 5) atom (list inside a list) (list 3
    4) (((very) (very) (very) (nested list))))

6
Basic Lisp types
  • Numbers (integers, floating-point, complex)
  • 27 -2 7.519
  • Characters, strings (arrays of chars)
  • \x \- \B
  • This is a string!
  • Symbols
  • a x jon
  • Lists (linked cells)
  • Empty list nil
  • (a b c) (2 3 jon)
  • cons structure has car (first) and cdr (rest)

7
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(list concatenation) nconc list
  • mapcar mapcan
  • find remove remove-if

8
Built-in functions (contd)
  • 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

9
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))

10
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

11
Reverse a list
  • (defun invert (mylist)
  • (inverte-aux mylist () )
  • )
  • (defun invert-aux (mylist outlist)
  • (if (null mylist)
  • outlist
  • (invert-aux (cdr mylist) (cons (car mylist)
    outlist))
  • )
  • )
  • (invert '(don patti blaz adam)) gt (ADAM BLAZ
    PATTI DON)

11
12
Lambda
  • (lambda (x) ( x 1))
  • ((lambda (x) ( x 1)) 4) gt 5
  • (defun my-f (mylist)
  • (if (null mylist)
  • ()
  • (cons
  • ((lambda (x) ( (- x 4) (- x 3))) (car mylist))
  • (my-f (cdr mylist))
  • )
  • )
  • )
  • (my-f'(1 2 3 4 5)) gt (6 2 0 0 2)

12
13
mapcar
  • (defun my-f-two (mylist)
  • (mapcar '(lambda (x) ( (- x 4) (- x 3)))
    mylist)
  • )
  • (my-f-two '(1 2 3 4 5)) gt (6 2 0 0 2)

13
14
Comic from xkcd.com
Write a Comment
User Comments (0)
About PowerShow.com