Lisp: Case Study ref: Lisp (3 rd Ed) Winston & Horn (Chapter 6) Donald F. Ross

Similar documents
AllegroCache Tutorial. Franz Inc

Exploring Strategies to Generate and Solve Sudoku Grids. SUNY Oswego CSC 466 Spring '09 Theodore Trotz

Heuristic Search with Pre-Computed Databases

Exercise 2-2. Four-Wire Transmitter (Optional) EXERCISE OBJECTIVE DISCUSSION OUTLINE. Ultrasonic level transmitter DISCUSSION

Annotated Bibliography: Artificial Intelligence (AI) in Organizing Information By Sara Shupe, Emporia State University, LI 804

Game Playing in Prolog

Part I At the top level, you will work with partial solutions (referred to as states) and state sets (referred to as State-Sets), where a partial solu

A Tic Tac Toe Learning Machine Involving the Automatic Generation and Application of Heuristics

For slightly more detailed instructions on how to play, visit:

Artificial Intelligence

Name. Part 2. Part 2 Swimming 55 minutes

NATHANIEL HAWTHORNE THE SCARLET LETTER

Spring 2007 final review in lecture page 1

Can Computers Think? an introduction to computer science, programming and artificial intelligence

Elements of a theory of creativity

CSC242 Intro to AI Spring 2012 Project 2: Knowledge and Reasoning Handed out: Thu Mar 1 Due: Wed Mar 21 11:59pm

Huckleberry Finn (Classics Illustrated) By Andrew Jay Hoffman, Mark Twain

CS 380: ARTIFICIAL INTELLIGENCE

Utopia (Penguin Classics) By Paul Turner, Thomas More READ ONLINE

The Intelligent Computer. Winston, Chapter 1

CS 4700: Foundations of Artificial Intelligence

The Complete Tales And Poems Of Edgar Allan Poe (Coterie Classics With Free Audiobook) By Edgar Allan Poe

CS10 : The Beauty and Joy of Computing

Intelligent Agents p.1/25. Intelligent Agents. Chapter 2

Spring 06 Assignment 2: Constraint Satisfaction Problems

Intelligent Modelling of Virtual Worlds Using Domain Ontologies

Computer Systems Research: Past and Future

The Discussion of this exercise covers the following points: Differential-pressure transmitter. Differential-pressure transmitter

Girls Programming Network. Scissors Paper Rock!

MAS336 Computational Problem Solving. Problem 3: Eight Queens


Running head: SIR ARTHUR CONAN DOYLE S INFLUENCE ON DETECTIVE FICTION 1

Introduction to Artificial Intelligence CS 151 Programming Assignment 2 Mancala!! Due (in dropbox) Tuesday, September 23, 9:34am

Patterson D W Artificial Intelligence

Educational Technology Lab

Level Lesson Plan Session 1

This game can be played in a 3x3 grid (shown in the figure 2.1).The game can be played by two players. There are two options for players:

o finally o another o second o after that o as a result o third o later o last o because o next o during o also o for example

Homework 5 Due April 28, 2017

Anchor Block Draft Tutorial

The power behind an intelligent system is knowledge.

Elements of a Story. Student Notes

CENTRO MARISTA DE LÍNGUAS

Statistical Hypothesis Testing

Spring 06 Assignment 2: Constraint Satisfaction Problems

TETRIS approach. Computing and Technology. On Campus - Full time May 2005

Name Score. 1. In Moby Dick, Ahab is an obsessed sea captain. In the Bible, Ahab is a.

CHAPTER I INTRODUCTION. of the key terms. Each point is presented as follows.

Computational Thinking

From Nothing to Something using AutoCAD Electrical

G51PGP: Software Paradigms. Object Oriented Coursework 4

CS 540: Introduction to Artificial Intelligence

Bibliography of Popov v Hayashi in AI and Law

Symbolic Computing With LISP And PROLOG By Robert A. Mueller

Lesson 53: Art/Museum Exhibitions (20-25 minutes)

Students will create a typed two-page or more sequel to Huckleberry Finn using Freytag s Plot Pyramid.

EARIN Jarosław Arabas Room #223, Electronics Bldg.

Step It Up a Rung from AutoCAD Designs to AutoCAD Electrical

Artificial Intelligence

DSP First. Laboratory Exercise #11. Extracting Frequencies of Musical Tones

Metodologías de Programación II Inteligencia Colectiva: Recomendaciones

BBC LEARNING ENGLISH 6 Minute English Will robots take our jobs?


Sheet Metal OverviewChapter1:

Signal Processing First Lab 20: Extracting Frequencies of Musical Tones

CS 152 Computer Programming Fundamentals Lab 8: Klondike Solitaire

Annex IV - Stencyl Tutorial

Short Fiction: From Stories to Sitcoms ENGL Summer 2017 / Session II / Mondays and Wednesdays

Start at 1 and connect all the odd numbers in order from least to greatest. Then start at 2 and connect all the even numbers the same way.

Computer Science Faculty Publications

UNIVERSITI MALAYSIA PERLIS

For many hundreds of years, literature has been one of the most important. human art forms. It allows us to give voice to our emotions, create

STARTING THE ENGINE: HOW TO MAKE THAT FIRST CHAPTER SPARKLE! Here s what I see with my writing students: Just about all first-timers, giddy with

CS10 : The Beauty and Joy of Computing

INFORMATION TECHNOLOGY AND LAWYERS

Programming Manual Washer Extractors Compass Control

Intro to Java Programming Project

101 Science Fiction Stories By Martin H. Greenberg READ ONLINE

SSR 10 MINUTES READ: LUCK BY MARK TWAIN Pg. 213

Artificial Intelligence : A New Synthesis By Nils.Nilsson

Digital Logic Design Nelson Manual Solutions

U.S. Cultural Movements of Early 1800s

The Impact of Artificial Intelligence. By: Steven Williamson

Frankenstein Or The Modern Prometheus - Large Print Edition By Mary Shelley, Sam Sloan

Sea Sonnets. David Hirzel. Click here if your download doesn"t start automatically

You Own It. Now Grow It!: 25 powerful relationship skills effective entrepreneurs use to grow successful businesses

More Recursion: NQueens

David Linthicum, Managing Director, Chief Cloud Strategy Officer, Deloitte Consulting LLP

Prince & The Pauper (Wordsworth Collection) By Mark Twain READ ONLINE

Background. Game Theory and Nim. The Game of Nim. Game is Finite 1/27/2011

DOWNLOAD OR READ : SUDOKU LARGE PRINT PUZZLE BOOK FOR ADULTS 200 MEDIUM PUZZLES PUZZLE BOOKS PLUS PDF EBOOK EPUB MOBI

AI in Business Enterprises

How AI & Deep Learning can help in Supply Chain Decision Making. By Krishna Khandelwal Chief Business Officer

Image Finder Mobile Application Based on Neural Networks

Manners with Technology (Monstrous Manners)

COMPLETE KOBOLD GUIDE

Read & Download (PDF Kindle) Common Lisp Recipes: A Problem-Solution Approach

Sex, Stories and Power Exchange: Adventures - And Lessons - Of A Naughty Power Exchange Couple

OOo Switch: 501 Things You Wanted to Know About Switching to OpenOffice.org from Microsoft Office

Would You Travel in Space? name. Graphic Organizer (before and during reading) Record the pros and cons of space travel as you read the text.

Transcription:

Lisp: Case Study ref: Lisp (3 rd Ed) Winston & Horn (Chapter 6) Donald F. Ross

Lisp: Case Study Exercise build a library system object: book collection:library title / author / class (attributes) data abstraction constructor (make) (sound familiar?) reader (get) writer (set) Program clichés (patterns) 2

Lisp: Case Study: the answer! (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))))) (defun book title (book) (second (assoc 'title book)) ) (defun book author (book) (second (assoc 'author book)) ) (defun book class (book) (second (assoc 'class book)) ) (defun fictionp (book) (member 'fiction (book class book)) ) (mapcar # <function> books) ; apply <function> to each element (remove if # <predicate> books) ; filter (find if # <predicate> books) ; find first (count if # <predicate> books) ; count 3

Lisp: Case Study: Comments Database A B C D E a b c d e x x Selection & projection Goals introduce Constructors Readers Writers Filters Find Count Functional thinking mapcar 4

Lisp: Case Study Design Aspects Association list Contains sub lists: symbol (key) + value (setf sarah ((height 1.65) (weight 65))) ((height 1.65) (weight 65)) Retrieval is performed by (assoc <key> <association list>) (assoc weight sarah) (weight 65) (second (assoc weight sarah)) 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! 5

Lisp: Case Study (setf book ex2 ( (title (Artificial Intelligence )) ; Title (author (Patrick Henry Winston)) ; Author (class (Technical AI )) ) ; Class ) Find the title (second (assoc title book ex2)) Find the author (second (assoc author book ex2)) Find the class (second (assoc class book ex2)) Which can be generalised (defun find (key object) (second (assoc key object))) (find title book ex2) (Artificial Intelligence) ; definition ; call 6

Lisp: Case Study reader readers define a reader for each key (attribute) (setf book ex2 ( (title (Artificial Intelligence )) ; Title (author (Patrick Henry Winston )) ; Author (class (Technical AI )) ) ; 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) 7

Lisp: Case Study constructor 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) (make book (Common Lisp) (Guy Steele) (Technical Lisp))) result: ((title (Common Lisp)) (author (Guy Steele)) (class (Technical Lisp))) 8

Lisp: Case Study constructor the constructor may be used in a writer or to create a DB of books (setf book ex make book( )) ; writer (list (make book ( ) (make book ( ) ) ; create DB This list can in turn be Transformed list of author names Filtered according to some predicate All fiction books ; selection (row) All authors ; projection (col) 9

Lisp: Case Study writer Writers >(setf book ex4 (make book '(Common Lisp) '(Guy Steele) '(Technical Lisp)) ) ((TITLE (COMMON LISP)) (AUTHOR (GUY STEELE)) (CLASS (TECHNICAL LISP))) >(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))) ((TITLE (COMMON LISP)) (AUTHOR (GUY L STEELE)) (CLASS (TECHNICAL LISP))) 10

Lisp: Case Study: Book Collection The Library Artificial Intelligence Patrick Henry Winston Technical AI Common Lisp Guy L Steele Technical Lisp Moby Dick Herman Melville Fiction Tom Sawyer Mark Twain Fiction The Black Orchid Rex Stout Fiction Mystery 11

Lisp: Case Study projection >(defun list authors (books) (if (endp books) nil ; empty list (cons (book author (first books)) ) ) >(list authors books) ; head / tail (list authors (rest books)) ) ; (cons A B) ((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT)) 12

Lisp: Case Study selection Find all the books in class fiction >(defun book class (book) (second (assoc 'class book))) >(book class (third books)) (fiction) >(defun fictionp (book) (member 'fiction (book class book)) ) >(fictionp (third books)) (fiction) Now use a programming cliché 13

Lisp: Case Study selection >(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))) )) ; otherwise ; no omit 1 2 3 ; (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: Case Study selection >(list fiction books 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)))) >(length (list fiction books books)) ;how many? 3 15

Lisp: Case Study apply all Lisp provides primitives to perform these actions (remove (filter), count & find (first)) mapcar apply a function to each list element (mapcar # oddp (1 2 3)) (T NIL T) List authors now becomes >(mapcar # book author books) ((PATRICK HENRY WINSTON) (GUY L STEELE) (HERMAN MELVILLE) (MARK TWAIN) (REX STOUT)) 16

Lisp: Case Study count Counters: count if / count if not >(count if # fictionp books) 3 >(count if not # fictionp books) 2 17

Lisp: Case Study find/remove Finders: find if / find if not finds the first occurrence Filters: remove if / remove if not >(find if # fictionp books) ((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)))) >(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

Lisp: Case Study: final version (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))))) (defun book title (book) (second (assoc 'title book)) ) (defun book author (book) (second (assoc 'author book)) ) (defun book class (book) (second (assoc 'class book)) ) (defun fictionp (book) (member 'fiction (book class book)) ) (mapcar # <function> books) ; apply <function> to each element (remove if # <predicate> books) ; filter (find if # <predicate> books) ; find first (count if # <predicate> books) ; count 19

Lisp: Case Study Lisp constructs primitive functions first, rest association list (assoc) setf mapcar remove if remove if not find if find if not count if count if not Clichés (patterns) Constructor Reader Writer Filter Find Count Apply function to each list element (mapcar) 20