Introduction to Artificial Intelligence CISC-481/681 Program 1: Simple Lisp Programs

Similar documents
LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS 842 Ben Cassell University of Waterloo

Allegro CL Certification Program

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Artificial Intelligence Programming

Introduction 2 Lisp Part I

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Lisp. Versions of LISP

UMBC CMSC 331 Final Exam

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

A little bit of Lisp

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

2. Reasons for implementing clos-unit

Common LISP Tutorial 1 (Basic)

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Lecture Notes on Lisp A Brief Introduction

INF4820. Common Lisp: Closures and Macros

Imperative, OO and Functional Languages A C program is

STRUCTURES AND STRATEGIES FOR STATE SPACE SEARCH

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Lecture #2 Kenneth W. Flynn RPI CS

(defmacro while (condition &body body) `(iterate loop () (if,condition (loop)))))

The Earley Parser

Part II Composition of Functions

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Homework & NLTK. CS 181: Natural Language Processing Lecture 9: Context Free Grammars. Motivation. Formal Def of CFG. Uses of CFG.

CS101 Introduction to Programming Languages and Compilers

Basic Structure of Denotational Definitions

Department of Computer and information Science Norwegian University of Science and Technology

LING 581: Advanced Computa7onal Linguis7cs. Lecture Notes April 16th

CSCI337 Organisation of Programming Languages LISP

This should prevent residual sexism, racism, favoritism lurking in the unconscious of your professor & TA from biasing grading!!

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Midterm Examination (Sample Solutions), Cmput 325

Associative Database Managment WIlensky Chapter 22

6.001 Notes: Section 15.1

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

A Brief Introduction to Common Lisp

CS 540-1: Introduction to Artificial Intelligence

#2 Madlibs. Print out a story using the five variables into a text box. Make a procedure called madlibs that works with a Main and a Start procedure.

Homework & Announcements

Principles of Programming Languages COMP251: Syntax and Grammars

Functions that return lists

CS A331 Programming Language Concepts

LING 130: Quantified Noun Phrases

A Quick Introduction to Common Lisp

UMBC CMSC 331 Final Exam

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Introduction to Lisp

by the evening of Tuesday, Feb 6

Statistics Case Study 2000 M. J. Clancy and M. C. Linn

Associative Database Managment

Variables, expressions and statements

An Interactive Desk Calculator. Project P2 of. Common Lisp: An Interactive Approach. Stuart C. Shapiro. Department of Computer Science

15 Unification and Embedded Languages in Lisp

1. He considers himself to be a genius. 2. He considered dieting to be unnecessary. 3. She considered that the waffle iron was broken. 4.

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS. Regrades 10/6/15. Prelim 1. Prelim 1. Expression trees. Pointers to material

Definite-clause grammars: PSGs in Prolog (in principle, string rewrite described as a logical formula).

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Robot Programming with Lisp

Semester Review CSC 301

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

Functional programming with Common Lisp

Intro to Haskell Notes: Part 5

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

GPS: The general problem solver. developed in 1957 by Alan Newel and Herbert Simon. (H. Simon, 1957)

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

A short note on short differentiation programs in lisp, and a comment on logarithmic differentiation

SCHEME AND CALCULATOR 5b

6.001 Notes: Section 8.1

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

Functional programming techniques

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

A Genetic Algorithm Implementation

(2 1) What does evaluate to? What does evaluate to? What does evaluate to?

CS 132 Exam #1 - Study Suggestions

Proseminar on Semantic Theory Fall 2013 Ling 720 An Algebraic Perspective on the Syntax of First Order Logic (Without Quantification) 1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Racket Style Guide Fall 2017

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Artificial Intelligence Lecture 1

COP 3402 Systems Software Syntax Analysis (Parser)

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Guidelines for Writing Mathematical Proofs

Lisp: Lab Information. Donald F. Ross

CS 275 Name Final Exam Solutions December 16, 2016

Trombone players produce different pitches partly by varying the length of a tube.

Microsoft PowerPoint Presentations

Transcription:

CISC-481/681 Program 1: Simple Lisp Programs 1 Introduction to Artificial Intelligence CISC-481/681 Program 1: Simple Lisp Programs Reading: Handouts Summarizing Common Lisp Functions Paul Graham s ANSI Common Lisp: Read Chapters 2, 3, 4, 5.1 5.4, 7.1 7.3, 14.5. his is about all you ll need for the entire course. More Lisp Practice Exercise 1 Write a function called palindromep that is given a list and returns t if the list has the same sequence of elements when read left to right as when read right to left. It should return NIL otherwise. his is a 2-line program, if you use the correct two functions. One is an equality function (either EQ, EQL, EQUAL, or =). Please explore and understand those functions we will use them often in the future. > (palindromep (a b c b a)) > (palindromep (go spot go)) > (palindromep ((x y z) (e f g) (e f g) (x y z))) >(palindromep (madam im adam)) NIL > (palindromep (x)) Exercise 2 Write a function called presentp that takes two arguments, an atom and a list. it should return non-nil if the atom appears anywhere in the list, and NIL if it does not appear. presentp is like member except that the atom can appear anywhere in the list, not just at top level. You should use recursion in your solution. Note that this is an example of writing a lisp program that operates on other lisp programs. > (setf formula (sqrt (/ (+ (expt x 2) (expt y 3)) 2.0))) (sqrt (/ (+ (expt x 2) (expt y 3)) 2.0)) > (presentp x formula) > (presentp z formula) NIL Exercise 3 Why does (member (a) ((a) (b))) return nil?

CISC-481/681 Program 1: Simple Lisp Programs 2 Exercise 4 What do the global lisp variables *print-level* and *print-length* do? Programming with ables Read the description of assoc in the book. We can use assoc to create a table of objects and decriptions (we could also use a hash table). Assume we store a table of descriptions in the global variable *things* (note that Common Lisp programmers use the asterisks to indicate that this is a global variable): (setf *things* ((o1 large green shiny cube) (o2 small red dull metal cube) (o3 red small dull plastic cube) (o4 small dull blue metal cube) (o5 small shiny red four-sided pyramid) (o6 large shiny green sphere))) Now we ll develop functions to tell us in which qualities two objects differ. We start by writing a function called description to retrieve the description of an object: (defun description (x) (rest (assoc x things))) >(description o3) --> (red small dull plastic cube) he differences between two objects are whatever properties appear in the description of the first but not the second, or the description of the second but not the first. he technical term for this is set-exclusive-or: (defun differences (x y) (set-exclusive-or (desciription x) (description y))) >(differences o2 o3) --> (metal plastic) While o1 is metal and o2 is plastic, we can classify properties according to the type of quality to which they refer. Here is such a table, represented as lists: (setf *quality-table* ((large size) (small size) (red color) (green color) (blue color) (shiny luster) (dull luster) (metal material) (plastic material) (cube shape) (sphere shape) (pyramid shape) (four-sided shape)))

CISC-481/681 Program 1: Simple Lisp Programs 3 We can use this table as part of a function that gives us the quality a given proerty refers to: (defun quality (x) (cadr (assoc x *quality-table*))) >(quality red) --> color >(quality large) --> size Exercise 5 Using differences and quality write a function quality-difference to tell us one quality that is different between a pair of objects >(quality-difference o2 o3) --> material >(quality-difference o1 o6) --> shape >(quality-difference o2 o4) --> color Exercise 6 Finally, write a functioncontrast that shows all of the differences between two objects, without any duplicates. >(conrast o3 o4) --> (color material) A Simple Example Lisp Program Study $CLASSHOME/prog1.lisp 1. he main function here is generate, and the style of programming used is called data-driven because the data (the list of rewrite rules pointed to by the variable *grammar*) drives what the program does next. It is a natural and easy-to-use style in Lisp, leading to concise and extensible programs, because it is always possible to add a new piece of data with a new association without having to modify the original program. Here are some examples of generate in use (try this yourself): CL-USER 2 : 2 > (generate sentence) (HE ABLE SAW A BALL) CL-USER 3 : 2 > (generate sentence) (HE BALL LIKED HE MAN) CL-USER 4 : 2 > (generate noun) (BALL) CL-USER 5 : 2 > (generate noun-phrase) (A WOMAN) CL-USER 6 : 2 > (generate verb-phrase) (OOK HE WOMAN) Another advantage of representing information in a declaritive form as rules or facts rather than as Lisp functions is that it can be easier to use the information for multiple purposes. We can easily write a program that generates not only the sentence, but the complete parsed syntax of the sentence: 1 From Peter Norvig s Paradigms of AI Programming

CISC-481/681 Program 1: Simple Lisp Programs 4 CL-USER 8 : 2 > (generate-tree sentence) (SENENCE (NOUN-PHRASE (ARICLE HE) (NOUN WOMAN)) (VERB-PHRASE (VERB LIKED) (NOUN-PHRASE (ARICLE HE) (NOUN ABLE)))) Or we can generate all the possible rewrites of a phrase CL-USER 9 : 2 > (generate-all noun-phrase) ((HE MAN) (A MAN) (HE BALL) (A BALL) (HE WOMAN) (A WOMAN) (HE ABLE) (A ABLE)) CL-USER 10 : 2 > (length (generate-all sentence)) 256 However, the function generate-all will only work for finite languages (note that the bigger grammar in the file defines an infinte language using recursive rules). Exercise 7 Switch to the bigger grammar in the file and generate a few sentences. Create your own grammar (for another natural language, or a subset of a computer language) and generate several sentences or programs. Excercise 8 Rewrite generate so that it still uses cond but it avoids calling rewrites twice. Excercise 9 Write a version of generate that explicitly differentiates between terminal symbols (those with no rewrite rules) and nonterminal symbols. For example, write a predicate function non-terminal-p and rewrite generate to use your new function. Your Own Short Lisp Program Excersise 10 Write a Lisp function ask-and-tell that processes simple sentences. It should allow you to enter lists that represent assertions, such as (Mary likes hiking) or (Steve hates pretzels), as well as questions, such as (Does Mary like hiking) or (Does Steve like pretzels). he assertions should all have a proper name (e.g., Mary) in the first position, the word likes or hates in the second position, and either an activity (e.g., hiking) or an object (e.g., pretzels) in the last position. he questions should be similar, except that they begin with the word Does. he function should remember the assertions you give it, and answer your questions appropriately. If you repeat an assertion, it should let you know that; if you enter a contradictory assertion, it should alert you to that, and confirm that you re sure before making the change. E.g.,

CISC-481/681 Program 1: Simple Lisp Programs 5 >(ask-and-tell (mary likes hiking)) >(ask-and-tell (steve hates pretzels)) "YES" >(ask-and-tell (does mary like pretzels)) "I DON KNOW" >(ask-and-tell (mary likes hiking)) "I KNOW HA ALREADY" >(ask-and-tell (does steve like pretzels)) "NO" >(ask-and-tell (mary hates hiking)) "YOU VE CHANGED YOUR MIND. ARE YOU SURE?" no "YES" >(ask-and-tell (mary hates hiking)) "YOU VE CHANGED YOUR MIND. ARE YOU SURE?" yes "NO" One easy way to do the database is to use Lisp s built-in hash table operations (see chapter 4.8 in Graham). Put the hashed DB in a global variable.