LISP – (LISt Processing)

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Introduction of Lisp function e.g. (+ 2 2) Shift+Enter Execute …… (+ 1 2) 3 ( ) 15 (- 1 6) -5 ( ) -5 (- ( ) (+
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
CS 462: Introduction to Artificial Intelligence This course advocates the physical-symbol system hypothesis formulated by Newell and Simon in It.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin.
Topics in Artificial Intelligence: Intelligent Problem Solvers This course is about building systems that can reason -- that is, solve problems by utilizing.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Curry A Tasty dish? Haskell Curry!. Curried Functions Currying is a functional programming technique that takes a function of N arguments and produces.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Introduction to Lisp
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Simple Lisp Programs References:
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Scheme : variant of LISP
Lists in Lisp and Scheme
LISP A brief overview.
6.001 SICP Data abstractions
First Lecture on Introductory Lisp
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Today’s topics Abstractions Procedural Data
Curry A Tasty dish? Haskell Curry!.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
CS 462: Introduction to Artificial Intelligence
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Presentation transcript:

LISP – (LISt Processing) 1. Parenthesized prefix notation: (+ 2 4) 2. Expressions can be nested: (- (+ 2 3 4 5) (* 2 5)) 3. Evaluation rule: apply function to value of arguments. 4. Symbolic computation: other types Primitive: numbers, characters, strings, symbols. Compound: lists. (append '(a b) '(c d)) --> (a b c d) Quote: block the evaluation of the following expression. '(a b) --> (a b) (a b) --> error: a undefined function. 5. Self-evaluating types: numbers, characters, strings. 6. Symbols as variables - not case sensitive. Quote append

VARIABLES 1. Define new objects in terms of others + name them. 2. (setf p '(a b)) --> (a b) p --> (a b) 3. Symbols also used to name functions. Quote

SPECIAL FORMS: 1. Forms not evaluated according to the evaluation rule. 2. Special forms: defun, defparameter, setf, let, case, if, function, quote.

LISTS 1. Primitive aggregating type. 2. Primitive functions: first, second, ..., length, last. append, cons, list.

DEFINING NEW FUNCTIONS: 1. (defun <name> (<parameters>) "doc" <body>) (defun last-name (name) "Select last name from a name represented as a list." (first (last name))) (last-name '(john r vandamme)) --> vandamme (last-name '(john quirk MD)) --> MD 2. Importance of abstraction. (defun first-name (name) (first name)) defun

USING FUNCTIONS: defparameter 1. (setf names '((john x ford) (peter p rabbit) (fabianna f wayne))) (mapcar #'last-name names) --> (ford rabbit wayne) #' from name of function to function object. mapcar primitive. 2. (defparameter *titles* '(Mr Mrs Miss Madam Ms Sir Dr Admiral Major General)) (defun first-name (name) "Select first name from a list representing a name." (if (member (first name) *titles*) (first-name (rest name)) (first name))) (if <test> <then-part> <else-part>) ;; explanation of if ;; example of first-name (first-name '(Madam Major General Paula Jones)) --> Paula 3. Trace functions defparameter Omits all titles from the name list, returns the first name.

HIGHER ORDER FUNCTIONS 1. Functions as first-class objects: can be manipulated, created, modified by running code. 2.Apply: (apply #'+ '(1 2 3 4)) --> 10 3. Funcall: (funcall #'+ 1 2) --> 3 (funcall #'+ '(1 2)) --> error (1 2) is not a number. 4. Function constructor: lambda. (lambda (parameter ...) body...): non atomic name of a function. ((lambda (x) (+ x 2)) 4) --> 6 (funcall #'(lambda (x) (* x 2)) 4) --> 8 ***Can create functions at run time.*** apply funcall lambda

OTHER DATA TYPES length 1. Strings: (length "abc") --> 3 2. Many number types. length

Basic terminology Atoms: word-like indivisible objects which can be numbers or symbols. Lists: sentence-like objects formed from atoms or other lists, and enclosed in parentheses. S-expressions: compositions of atoms and lists. Procedures: step by step specifications how to do something. Primitives: procedures supplied by the LISP itself Example: (+ 5 6) User-defined procedures: procedures introduced by the programmer. Example: (students 'anna) Program: a collection of procedures working together.

S-expressions An s-expression can have other s-expressions nested in it. Examples: (+ (* 5 7) ( / 2 4)) (This (is a dog) (or a cat)) Expressions can be interpreted both, procedurally and declaratively. If interpreted procedurally, an expression provides a direction for doing something. Such an expression is called a form, and its first element is the name of a procedure to be used to produce the value. The process of computing the value of an expression is called evaluation. If interpreted declaratively, expressions represent data. Data and procedures have the same syntax.

Evaluation of atoms The value of a number is the number itself. Example: 5 ==> 5 The value of a string is the string itself. Example: “Nice day” ==> “Nice day” The value of the symbol T is T (true). The value of the symbol NIL is NIL (false). The symbol NIL and the empty list ( ) are the same thing. Variables are names of memory locations. The contents stored in a given memory cell is the value of the variable serving as a name of this location. Example: Let x be a variable, and 5 be the contents of the memory cell called x. Then, the value of x is 5.

Numbers Integers: 179, 45 Ratio: 5/7, 7/9 Floating point: 5.2, 7.9 Examples: * (/ 25 5) 5 * (/ 46 9) 46/9 ; do not divide evenly * (float (/ 46 9)) 5.111111 * (round (/ 46 9)) 5 ; the nearest integer 1/9 ; the remainder

More numeric primitives * (- 6) -6 * (- -6) 6 * (max 5 7 2) 7 * (min 5 7 2) 2 * (sqrt (* (+ 1 3) (* 2 2))) 4.0 * (+ (round (/ 22 7)) (round (/ 7 3))) 5 * (+ 2 2.5) 4.5 * (expt 3 6) 729 * (sqrt 81) 9.0 * (sqrt 82) 9.055386 * (abs 6) 6 * (abs -6)

LISP EVALUATION RULE 1. Every expression is either a list or an atom. 2. Every list to be evaluated is either a special form or a function application. 3. A special form expression is a list whose first element is a special form operator and is evaluated according to the special rule of that operator. 4. A function application is evaluated by first evaluating the arguments (the rest of the list) and then finding the function named by the first element of the list and applying it to the list of evaluated arguments. 5. Every atom is either a symbol or a non-symbol. 6. A symbol evaluates to the most recent value assigned to the variable. 7. A non-symbol atom evaluates to itself.

WHAT MAKES LISP DIFFERENT 1. Built-in support for Lists. 2. Automatic memory management. 3. Dynamic typing. 4. First-class functions. 5. Uniform syntax. 6. Extensibility

Why LISP? Especially designed for symbol manipulation. Provides built-in support for lists (“everything is a list..”) Automatic storage management (no need to keep track of memory allocation). Interactive environment, which allows programs to be developed step by step. That is, if a change is to be introduced, only changed functions need to be recompiled.

Everything's a List! (a b c) (defun plus (x y) (+ x y)) Data Functions Simple syntax: (function-name arg1 arg2 …) (defun plus (x y) (+ x y))

LISP is Interpreted and interactive USER(1): 12 12 USER(2): (+ 12 3) 15 USER(3): (setf Almost-age 31) 31 USER(4): Almost-age USER(5): 'Almost-age ALMOST-AGE USER(6):

Lisp is Symbolic Why do we care about symbols? Understand human cognition with a language like symbolic processing

Physical Symbol System Hypothesis "A physical symbol system has the necessary and sufficient means for general intelligent action." (Newell & Simon 1976) Physical symbol system Set of entities called symbols - physical patterns that can occur as components Expressions (or symbol structures) built of symbols

Lisp is Dynamic USER(1): (+ 1 2 3) 6 USER(2): (apply #'+ '(1 2 3)) 6 Functions are first-class objects Pass functions as arguments to other functions USER(1): (+ 1 2 3) 6 USER(2): (apply #'+ '(1 2 3)) 6

Funcall and Lambda – dynamic Create functions on the fly Lisp contains itself: eval USER(1): (funcall #'(lambda (x y) (+ x y)) 17 14) 31

Name Calling Lisp remembers function names separately from variable names USER(22): (defun add (x y) (+ x y)) ADD USER(23): (setf add 9) 9 USER(24): add 9 USER(25): #'add #<Interpreted Function ADD>

Problems with solutions: Write a function (power 3 2) = 3^2 = 9 2. Write a function that counts the number of atoms in an expression. (count-atoms '(a (b) c)) --> 3 3. (count-anywhere 'a '(a ((a) b) a)) --> 3 4. (dot-product '(10 20) '(3 4)) --> 10x3 + 20x4 = 110 5. Write a function (flatten '(a (b) () ((c)))) --> (a b c) which removes all levels of parenthesis and returns a flat list of atoms. 6. Write a function (remove-dups '(a 1 1 a b 2 b)) --> (a 1 b 2) which removes all duplicate atoms from a flat list. (Note: there is a built-in remove-duplicates in Common Lisp, do not use it).

Solutions 1-3 Write a function (power 3 2) = 3^2 = 9 (defun power (a b) "compute a^b - (power 3 2) ==> 9" (if (= b 0) 1 (* a (power a (- b 1))))) (defun count-atoms (exp) "count atoms in expresion - (count-atoms '(a (b) c)) ==> 3" (cond ((null exp) 0) ((atom exp) 1) (t (+ (count-atoms (first exp)) (count-atoms (rest exp)))))) (defun count-anywhere (a exp) "count performances of a in expresion - (count-anywhere 'a '(a ((a) b) (a))) ==> 3" ((atom exp) (if (eq a exp) 1 0)) (t (+ (count-anywhere a (first exp)) (count-anywhere a (rest exp)))))) Write a function that counts the number of atoms in an expression. (count-atoms '(a (b) c)) --> 3 (count-anywhere 'a '(a ((a) b) a)) --> 3

Solutions (defun flatten (exp) Write a function (flatten '(a (b) () ((c)))) --> (a b c) which removes all levels of parenthesis and returns a flat list of atoms. (defun flatten (exp) "removes all levels of paranthesis and returns flat list of atoms (flatten '(a (b) () ((c)))) ==> (a b c)" (cond ((null exp) nil) ((atom exp) (list exp)) (t (append (flatten (first exp)) (flatten (rest exp))))))

A tree structure of nodes for data base EXAMPLE A tree structure of nodes for data base

Example of interaction with the program that will be shown: You type How it works? You have some item in mind, program asks, you respond. (questions) Is it a ANIMAL? (yes) Is it a MAMMAL? (yes) I give up - what is it? (whale) #S( NODE :NAME WHALE :YES NIL :NO NIL ) Is it a ANIMAL? (no) Is it a VEGETABLE? (no) Is it a MINERAL? (no) I give up - what is it? (fruit) #S( NODE :NAME FRUIT :YES NIL :NO NIL ) Is it a FRUIT? (it) AHA! Program responds Program learned this. Now it knows that whale is a mammal. You confirm that the program guessed correctly Program learned this. Now it knows that it is a fruit

e.g. 3: A tree structure of nodes for data base A data base called *db*. The data base is organized into a tree structure of nodes. Each node has three fields: the name of the object it represents, a node to go to if the answer is yes, and a node for when the answer is no. We traverse the nodes until we either get an “it” reply or have to give up. “It” means that we found a correct answer in our questioning. (defstruct node name (yes nil) (no nil)) NODE (defvar *db* (make-node :name 'animal :yes (make-node :name 'mammal) :no (make-node :name 'vegetable :no (make-node :name 'mineral)))) *DB* animal yes no mammal vegetable no node-name mineral node-yes node-yes node-no

Makes new node in the tree (defun give-up () (format t "~&I give up - what is it?") (make-node :name (first (read)))) GIVE-UP (defun questions (&optional (node *db*)) (format t "~&Is it a ~a? " (node-name node)) (case (first (read)) ((y yes) (if (not (null (node-yes node))) (questions (node-yes node)) (setf (node-yes node) (give-up)))) ((n no) (if (not (null (node-no node))) (questions (node-no node)) (setf (node-no node) (give-up)))) (it 'aha!) (t (format t "reply with YES, NO, or IT if I have guessed it.") (questions node)))) QUESTIONS Makes new node in the tree Returns name of function as a value Calls itself recursively Sets structure

Execution results: animal no yes mammal vegetable yes no whale yes (questions) Is it a ANIMAL? (yes) Is it a MAMMAL? (yes) I give up - what is it? (whale) #S( NODE :NAME WHALE :YES NIL :NO NIL ) Is it a ANIMAL? (no) Is it a VEGETABLE? (no) Is it a MINERAL? (no) I give up - what is it? (fruit) #S( NODE :NAME FRUIT :YES NIL :NO NIL ) Is it a FRUIT? (it) AHA! animal yes no mammal vegetable yes no whale yes mineral fruit IT!

A simple lisp program of generating English sentences EXAMPLE: A simple lisp program of generating English sentences

e.g. 4: A simple lisp program of generating English sentences (defun random-elt (choices) "Choose an element from a list at random" (elt choices (random (length choices)))) RANDOM-ELT (defun one-of (set) "pick one element of set and make a list of it" (list (random-elt set))) ONE-OF (defun Verb () (one-of '(hit took saw liked))) VERB (defun Noun () (one-of '(man ball woman table))) NOUN (defun Article () (one-of '(the a))) ARTICLE

Pictures drawn by a robot Dances Music composed (defun verb-phrase () (append (Verb) (noun-phrase) )) VERB-PHRASE (defun noun-phrase () (append (Article) (Noun))) NOUN-PHRASE (defun sentence () (append (noun-phrase ) (verb-phrase))) SENTENCE (sentence) (THE MAN SAW THE TABLE) (A TABLE SAW THE BALL) (A MAN SAW THE BALL) Similarly, you can create “sentences” in any kind of “structured” set, which is a language: Motions Facial gestures Pictures drawn by a robot Dances Music composed

A rule-based solution of generating English sentences EXAMPLE: A rule-based solution of generating English sentences

e.g. 5: A rule-based solution of generating English sentences (defparameter *simple-grammar* '((sentence -> (noun-phrase verb-phrase)) (noun-phrase -> (ar Noun)) (verb-phrase -> (Verb noun-phrase)) (ar -> the a) (Noun -> man ball woman table) (Verb -> hit took saw liked)) "A grammar for a trivial subset of English.") *SIMPLE-GRAMMAR* (defvar *grammar* *simple-grammar* "The grammar used by generate. Initially, this is *simple-grammar*, but we can switch to other grammars.") *GRAMMAR* (defun rule-lhs (rule) "The left-hand side of a rule" (first rule)) RULE-LHS

(defun rule-rhs (rule) "The right-hand side of a rule" (rest (rest rule))) RULE-RHS (defun rewrite(category) "return a list of the possible rewritees for this category" (rule-rhs (assoc category *grammar*))) REWRITE (defun random-elt (choices) "choose an element from a list at random" (elt choices (random (length choices)))) RANDOM-ELT (defun mappend (fn the-list) "Apply fn to each element of list and append the result" (apply #'append (mapcar fn the-list))) MAPPEND

(defun generate (phrase) "Generate a random sentence or a phrase" (cond ((listp phrase) (mappend #'generate phrase)) ((rewrite phrase) (generate (random-elt (rewrite phrase)))) (t (list phrase)))) GENERATE (generate 'sentence) (THE WOMAN LIKED A TABLE) (generate 'noun-phrase) (A WOMAN) (generate 'verb-phrase) (TOOK A WOMAN) (generate 'Verb) (HIT) (generate 'Noun) (BALL) (generate 'Article) (THE)

Exercise Problems If you have time, try to do the following exercise problems. Modify e.g.3 and make a data base of English words: verb (intransitive verb, transitive verb), noun, article, adjective, adverb, preposition, …, etc. Write a version of generate function that explicitly differentiates between terminal symbols (those with no rewrite rules) and non terminal symbols. Defining a new grammar that includes adjectives, prepositional phrases, proper names, and pronouns.

Simple Lisp Programs AI: Exercise 5 References: http://www-itolab.ics.nitech.ac.jp/LISP/text-contents.html http://grimpeur.tamu.edu/~colin/lp/lp.html http://www-2.cs.cmu.edu/Groups/AI/html/cltl/clm/#R http://www.cs.utsa.edu/research/AI/cltl/clm/clm.html

http://clisp.cons.org/ http://sourceforge.net/projects/clisp