Lisp:CaseStudy ref:Lisp(3rd Ed)Winston&Horn(Chapter6) Donald F. Ross Lisp:CaseStudy • title / author / class collection: library (attributes) • data abstraction • constructor • reader • writer • Program “clichés” (make) (get) (set) (patterns) (sound familiar?) PS - Lisp Case study • object: book 28/03/2017 • Exercise – build a library system 2 (defun book‐title (book) (defun book‐author (book) (defun book‐class (book) (defun fictionp (book) (second (assoc 'title book)) ) (second (assoc 'author book)) ) (second (assoc 'class book)) ) (member 'fiction (book‐class book)) ) (mapcar #’<function> books) (remove‐if #’<predicate> books) (find‐if #’<predicate> books) (count‐if #’<predicate> books) ; apply <function> to each element ; filter ; find first ; count PS - Lisp Case study (defun make‐book (title author class) (defun baw (book author) (list (list 'title title) (if (eql 'author (first (first book))) (list 'author author) (cons (list 'author author) (rest book)) (list 'class class) )) (cons (first book) (baw (rest book) author))))) 28/03/2017 Lisp:CaseStudy:theanswer! 3 Lisp:CaseStudy:Comments A B C D E a b c d e ‐ ‐ ‐ ‐ ‐ ‐ x ‐ ‐ ‐ ‐ x ‐ ‐ ‐ ‐ ‐ ‐ ‐ ‐ • Selection & projection • • • • • • • • Constructors Readers Writers Filters Find Count Functional thinking mapcar 28/03/2017 • Goals – introduce PS - Lisp Case study • Database 4 • (setf sarah ‘((height 1.65) (weight 65))) ((height 1.65) (weight 65)) • Retrieval is performed by • (assoc <key> <association list>) • (assoc ‘weight sarah) • (second (assoc ‘weight sarah)) (weight 65) 65 • Advantages of a reader • • • • The data structure can be changed Only the (internal) code for the reader need be changed The functionality remains the same book‐author: book author You all knew that! PS - Lisp Case study • Association list • Contains sub‐lists: symbol (key) + value 28/03/2017 Lisp:CaseStudyDesignAspects 5 • Find the title • Find the author • Find the class ; Title ; Author ; Class (second (assoc ‘title book‐ex2)) (second (assoc ‘author book‐ex2)) (second (assoc ‘class book‐ex2)) • Which can be generalised PS - Lisp Case study (setf book‐ex2 ‘( (title (Artificial Intelligence )) (author (Patrick Henry Winston)) (class (Technical AI )) ) ) 28/03/2017 Lisp:CaseStudy (defun find (key object) (second (assoc key object))) ; definition (find ‘title book‐ex2) ; call (Artificial Intelligence) 6 (attribute) (setf book‐ex2 ‘( (title (Artificial Intelligence )) (author (Patrick Henry Winston )) (class (Technical AI )) ) ; Title ; Author ; Class ) (defun book‐author (book) (second (assoc ‘author book)) (defun book‐title (book) (second (assoc ‘title book)) (defun book‐class (book) (second (assoc ‘class book)) function name‐‐‐‐‐parameter‐‐‐‐‐‐‐function body‐‐‐‐‐‐‐‐‐‐‐‐ Application (function call): (book‐author book‐ex2) Value (application result): (Patrick Henry Winston) PS - Lisp Case study • readers – define a reader for each key 28/03/2017 Lisp:CaseStudy– reader 7 (make‐book ‘(Common Lisp) ‘(Guy Steele) ‘(Technical Lisp))) result:‐ ((title (Common Lisp)) (author (Guy Steele)) (class (Technical Lisp))) PS - Lisp Case study • Constructor (note: literal symbols & parameter VALUES) (defun make‐book (title author class) (list (list ‘title title ) (list ‘author author ) (list ‘class class ))) >(setf book‐ex4 ; NB this is a writer (setter) 28/03/2017 Lisp:CaseStudy‐ constructor 8 • (setf book‐ex make‐book(‐‐‐)) ; writer • (list (make‐book (‐‐‐) (make‐book (‐‐‐) …) ; create DB • This list can in turn be • Transformed • Filtered • All fiction books • All authors PS - Lisp Case study • the constructor may be used in a writer • or to create a DB of books 28/03/2017 Lisp:CaseStudy‐ constructor – list of author names – according to some predicate ; selection (row) ; projection (col) 9 Lisp:CaseStudy‐ writer • Writers >(defun book‐author‐writer (book author) (if (eql 'author (first (first book))) (cons (list 'author author) (rest book)) (cons (first book) (book‐author‐writer (rest book) author))) ) >(setf book‐ex4 (book‐author‐writer book‐ex4 '(Guy L Steele))) PS - Lisp Case study ((TITLE (COMMON LISP)) (AUTHOR (GUY STEELE)) (CLASS (TECHNICAL LISP))) 28/03/2017 >(setf book‐ex4 (make‐book '(Common Lisp) '(Guy Steele) '(Technical Lisp)) ) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP))) 10 Lisp:CaseStudy:BookCollection Patrick Henry Winston Common Lisp Guy L Steele Moby Dick Herman Melville Technical AI Technical Lisp Fiction Tom Sawyer Mark Twain Fiction The Black Orchid Rex Stout Fiction Mystery PS - Lisp Case study Artificial Intelligence 28/03/2017 • The Library 11 ((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT)) PS - Lisp Case study >(defun list‐authors (books) ; empty list (if (endp books) nil (cons (book‐author (first books)) ; head / tail (list‐authors (rest books)) ) ; (cons A B) ) ) >(list‐authors books) 28/03/2017 Lisp:CaseStudy‐ projection 12 Lisp:CaseStudy‐ selection >(defun fictionp (book) (member 'fiction (book‐class book)) ) >(fictionp (third books)) (fiction) Now use a programming cliché… PS - Lisp Case study >(defun book‐class (book) (second (assoc 'class book))) >(book‐class (third books)) (fiction) 28/03/2017 Find all the books in class “fiction” 13 ; otherwise ; no – omit 1 2 3 PS - Lisp Case study >(defun list‐fiction‐books (books) (cond ((endp books) nil) ; empty list? ((fictionp (first books)) ; is fiction? (cons (first books) ; yes ‐ include (list‐fiction‐books (rest books)))) (t (list‐fiction‐books (rest books))) 28/03/2017 Lisp:CaseStudy‐ selection )) ; (1) empty list (2) first book is fiction (3) default (t = true) cond is if (A) exprA … elsif (B) exprB… else expr_default (logical switch!) (cond (predicate1 expr1) (predicate2 expr2) … (t expr_default)) 14 Lisp:CaseStudy‐ selection PS - Lisp Case study (((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY)))) 28/03/2017 >(list‐fiction‐books books) >(length (list‐fiction‐books books)) ;how many? 3 15 • mapcar – apply a function to each list element (mapcar #’oddp ‘(1 2 3)) (T NIL T) • List‐authors now becomes >(mapcar #’book‐author books) PS - Lisp Case study • Lisp provides primitives to perform these actions (remove (filter), count & find (first)) 28/03/2017 Lisp:CaseStudy– applyall ((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT)) 16 Lisp:CaseStudy‐ count #’fictionp books) >(count‐if‐not #’fictionp books) 2 PS - Lisp Case study >(count‐if 3 28/03/2017 • Counters: count‐if / count‐if‐not 17 ((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) >(find‐if‐not #’fictionp books) ((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) >(remove‐if #’fictionp books) (((TITLE (ARTIFICIAL INTELLIGENCE)) (AUTHOR (PATRICK HENRY WINSTON)) (CLASS (TECHNICAL AI))) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP)))) PS - Lisp Case study • Finders: find‐if / find‐if‐not ‐ finds the first occurrence • Filters: remove‐if / remove‐if‐not >(find‐if #’fictionp books) 28/03/2017 Lisp:CaseStudy– find/remove >(remove‐if‐not #’fictionp books) (((TITLE (MOBY DICK)) (AUTHOR (HERMAN MELVILLE)) (CLASS (FICTION))) ((TITLE (TOM SAWYER)) (AUTHOR (MARK TWAIN)) (CLASS (FICTION))) ((TITLE (THE BLACK ORCHID)) (AUTHOR (REX STOUT)) (CLASS (FICTION MYSTERY)))) 18 (defun book‐title (book) (defun book‐author (book) (defun book‐class (book) (defun fictionp (book) (second (assoc 'title book)) ) (second (assoc 'author book)) ) (second (assoc 'class book)) ) (member 'fiction (book‐class book)) ) (mapcar #’<function> books) (remove‐if #’<predicate> books) (find‐if #’<predicate> books) (count‐if #’<predicate> books) ; apply <function> to each element ; filter ; find first ; count PS - Lisp Case study (defun make‐book (title author class) (defun baw (book author) (list (list 'title title) (if (eql 'author (first (first book))) (list 'author author) (cons (list 'author author) (rest book)) (list 'class class) )) (cons (first book) (baw (rest book) author))))) 28/03/2017 Lisp:CaseStudy:finalversion 19 Lisp:CaseStudy • first, rest • • • • • • association list (assoc) setf mapcar remove‐if remove‐if‐not find‐if find‐if‐not count‐if count‐if‐not • • • • • • • Constructor Reader Writer Filter Find Count Apply function to each list element (mapcar) 28/03/2017 • primitive functions • Clichés – (patterns) PS - Lisp Case study • Lisp constructs 20
© Copyright 2025 Paperzz