KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  15.1.1 Symbolic Expressions, the Syntactic.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
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.
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
COMMON LISP Symbolic Expressions The syntactic elements of the LISP programming language are symbolic expressions. –Known as s-expressions. –Both programs.
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).
Introduction CSC 358/458 3/27/2006. Outline Introductions Course goals Course organization History of Lisp Lisp syntax Examples Lisp IDE.
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)
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
TES3111 October 2001 Artificial Intelligence LISP.
An Introduction to LISP
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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.
COMMON LISP: A Gentle Introduction to Symbolic Computation
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
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.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
LISP John McCarthy and Marvin Minsky formed MIT’s AI Project in Suggested reading: John McCarthy’s home page
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Control in LISP More on Predicates & Conditionals.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
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
Functional Programming Languages
Functional Programming
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
CS 326 Programming Languages, Concepts and Implementation
LISP LISt Processing.
LISP A brief overview.
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
LISP LISt Processing.
Abstraction and Repetition
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
15.2 Mathematical Functions
Common Lisp II.
Lisp.
LISP primitives on sequences
Presentation transcript:

KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic Basis of LISP  Control of LISP Evaluation: quote and eval  Programming in LISP: Creating New Functions  Program Control in LISP: Conditionals and Predicates  Functions, Lists, and Symbolic Computing  Lists as Recursive Structures  Nested Lists, Structure, and car/cdr Recursion  Binding Variables Using set  Defining Local Variables Using let  Data Types in Common LISP

KU NLP Artificial Intelligence Characteristics of LISP 1. Language for artificial intelligence programming a. Originally designed for symbolic computing 2. Imperative language a. Describe how to perform an algorithm b. Contrasts with declarative languages such as PROLOG 3. Functional programming a. Syntax and semantics are derived from the mathematical theory of recursive functions. b. Combined with a rich set of high-level tools for building symbolic data structures such as predicates, frames, networks, and objects 4. Popularity in the AI community a. Widely used as a language for implementing AI tools and models b. High-level functionality and rich development environment make it an ideal language for building and testing prototype systems.

KU NLP Artificial Intelligence LISP: A Brief Overview q Syntactic elements of LISP  Symbolic expressions : S-expressions 3 Atom : basic syntactic units 3 List  Both programs and data are represented as s-expressions q Atom  Letters (upper, lower cases)  Numbers  Characters : * + - $ % ^ & _ ~.  Example Hyphenated-name 3 *some-global* 3 nil 3x3x

KU NLP Artificial Intelligence4 List and S-expression q List  A sequence of either atoms or other lists separated by blanks and enclosed in parentheses. 3 Example 2 ( ) 2 (a (b c) (d (e f)))  Empty list “( )” : nil 3 nil is the only s-expression that is considered to be both an atom and a list. q S-expression  An atom is an s-expression.  If s 1, s 2,…, s n are s-expressions, then so is the list (s 1 s 2 … s n ).

KU NLP Artificial Intelligence5 Read-Eval-Print q Interactive Environment  User enters s-expressions  LISP interpreter prints a prompt q If you enter  Atom: LISP evaluates itself (error if nothing is bound to the atom)  List: LISP evaluates as an evaluation of function, i.e. that the first item in the list needs to be a function definition (error if no function definition is bound for the first atom), and remaining elements to be its arguments. 3 > (* 7 9) 63

KU NLP Artificial Intelligence6 Control of LISP Evaluation: quote & eval  quote : ‘  prevent the evaluation of arguments 3 (quote a) ==> a 3 (quote (+ 1 3)) ==> (+ 1 3) 3 ‘a ==> a 3 ‘(+ 4 6) ==> (+ 4 6) q eval  allows programmers to evaluate s-expressions at will 3 (eval (quote (+ 2 3))) ==> 5 3 (list ‘* 2 5) ==> (* 2 5) 3 (eval (list ‘* 2 5)) ==> 10

KU NLP Artificial Intelligence7 Programming in LISP: Creating New Functions q Syntax of Function Definition  (defun ( ) ) 3 defun : define function q Example  (defun square (x) (* x x))  (defun hypotenuse (x y) ;the length of the hypotenuse is (sqrt (+ (square x) ;the square root of the sum of (square y)))) ;the square of the other sides.

KU NLP Artificial Intelligence8 Program Control in LISP: Conditionals & Predicates(1/2) q Conditions  (cond ( ) ( )... ( <action n))  Evaluate the conditions in order until one of the condition returns a non-nil value  Evaluate the associated action and returns the result of the action as the value of the cond expression q Predicates  Example 3 (oddp 3) ; whether its argument is odd or not 3 (minusp 6) 3 (numberp 17)

KU NLP Artificial Intelligence9 Conditionals & Predicates(2/2) q Alternative Conditions  (if test action-then action-else) 3 (defun absolute-value (x) (if (< x 0) (- x) x)) 3 it returns the result of action-then if test return a non-nil value  it return the result of action-else if test returns nil  (and action1... action-n) ; conjunction 3 Evaluate arguments, stopping when any one of arguments evaluates to nil  (or action1... action-n) ; disjunction 3 Evaluate its arguments only until a non-nil value is encountered

KU NLP Artificial Intelligence10 List as recursive structures q Cons cell: data structure to hold a list in LISP  car - holds the first element.  cdr - holds the rest in the list. q Accessing a list  (car ‘(a b c)) ==> a  (cdr ‘(a b c)) ==> (b c)  (first ‘(a b c)) ==> a  (second ‘(a b c)) ==> b  (nth 1 ‘(a b c)) ==> b q Constructing a list  (cons ‘a ‘(b c)) ==> (a b c)  (cons ‘a nil) ==> (a)  (cons ‘(a b) ‘(c d)) ==> ((a b) c d)

KU NLP Artificial Intelligence11 Nested lists, structure, car/cdr recursion q More list construction  (append ‘(a b) ‘(c d)) ==> (a b c d)  (cons ‘(a b) ‘(c d)) ==> ((a b) c d) q Counting the number of elements in the list  (length ‘((1 2) 3 (1 (4 (5))))) ==> 3

KU NLP Artificial Intelligence12 Binding variables: set(1/2) q (setq )  bind to  is NOT evaluated. q (set )  replace s-expression at with  is evaluated. (it must exists.) q (setf )  generalized form of set: when is a symbol, it behaves like setq; otherwise, it behaves like set.

KU NLP Artificial Intelligence13 q Examples of set / setq  (setq x 1) ==> 1 ;;; assigns 1 to x  (set a 2) ==> ERROR!! ;;; a is NOT defined  (set ‘a 2) ==> 2 ;;; assigns 2 to a  (+ a x) ==> 3  (setq l ‘(x y z)) ==> (x y z)  (set (car l) g) ==> g  l ==> (g y z) q Examples of setf  (setf x 1) ==> 1  (setf a 2) ==> 2  (+ a x) ==> 3  (setf l ‘(x y z)) ==> (x y z)  (setf (car l) g) ==> g  l ==> (g y z) Binding variables: set(2/2)

KU NLP Artificial Intelligence14 Local variables: let(1/2) q Consider a function to compute roots of a quadratic equation: ax 2 +bx+c=0  (defun quad-roots1 (a b c) (setq temp (sqrt (- (* b b) (* 4 a c)))) (list (/ (+ (- b) temp) (* 2 a)) (/ (- (- b) temp) (* 2 a))))  (quad-roots ) ==> ( )  temp ==> 0.0  Local variable declaration using let  (defun quad-roots2 (a b c) (let (temp) (setq temp (sqrt (- (* b b) (* 4 a c)))) (list (/ (+ (- b) temp) (* 2 a)) (/ (- (- b) temp) (* 2 a)))))

KU NLP Artificial Intelligence15 q Any variables within let closure are NOT bound at top level. q More improvement (binding values in local variables)  (defun quad-roots3 (a b c) (let ((temp (sqrt (- (* b b) (* 4 a c)))) ((denom (*2 a))) (list (/ (+ (- b) temp) denom) (/ (- (- b) temp) denom)))) Local variables: let(2/2)

KU NLP Artificial Intelligence16 For Homework q Representing predicate calculus with LISP q ∀ x likes (x, ice_cream) (Forall (VAR X) (likes (VAR X) ice_cream)) q ∃ x foo (x, two, (plus two three)) (Exist (VAR X) (foo (VAR X) two (plus two three)))  Connectives  ¬ S (nott S)  S 1 ∧ S 2 (ANDD S 1 S 2 )  S 1 ∨ S 2 (ORR S 1 S 2 )  S 1 → S 2 (imply S 1 S 2 )  S 1 ≡ S 2 (equiv S 1 S 2 ) q Do not use the builtin predicate names.

KU NLP Artificial Intelligence17 For Homework q 숙제를 간략하게 하기 위해서 다음 predicate 만을 atomic sentence 라고 가정한다.  true, false  likes (george, kate)likes (x, george) likes (george, susie)likes (x, x) likes (george, sarah, tuesday) friends (bill, richard) friends (bill, george) friends (father_of(david), father_of(andrew)) helps (bill, george) helps (richard, bill) equals (plus (two, three), five)

KU NLP Artificial Intelligence18 For Homework (DEFUN atomic_s (Ex) (cond ((member Ex ‘(true false)) T) ((member (car Ex) ‘(likes …)) (Test_term (cdr Ex))) (T nil))) (DEFUN Test_term (ex1) (cond ((NULL ex1) T) ((member (car ex1) ‘(george …..) (test_term (cdr ex1))) ((VARP (car ex1)) (test_term (cdr ex1))) ((functionp (car ex1)) (test_term (cdr ex1))) (T nil)))

KU NLP Artificial Intelligence19 Recursion Templates q Recursion template  A few standard forms of recursive Lisp functions.  You can create new functions by choosing a template and filling in the blanks. q Recursion templates 의 종류 ① Double-Test Tail Recursion ② Single-Test Tail Recursion ③ Single-Test Augmenting Recursion ④ List-Consing Recursion ⑤ Simultaneous Recursion on Several Variables ⑥ Conditional Augmentation ⑦ Multiple Recursion ⑧ CAR/CDR Recursion

KU NLP Artificial Intelligence20 Double-Test Tail Recursion q Template : (DEFUN func (X) (COND (end-test-1 end-value-1) (end-test-2 end-value-2) (T (func reduced-x)))) q Example : Func :ANYODDP End-test-1 : (NULL X) End-value-1 : NIL End-test-2 : (ODDP(FIRST X)) End-value-2 :T Reduced-x :(REST X) (defun anyoddp (x) (cond ((null x) nil) ((oddp (first x)) t) ((t (anyoddp (rest x))))

KU NLP Artificial Intelligence21 Single-Test Tail Recursion q Template : (DEFUN func (X) (COND (end-test end-value) (T (func reduced-x)))) q Example : Func :FIND-FIRST-ATOM End-test : (ATOM X) End-value :X Reduced-x :(FIRST X) (defun find-first-atom (x) (cond ((atom x) x) ((t (find-first-atom (first x))))

KU NLP Artificial Intelligence22 Single-Test Augmenting Recursion(1/2) q Template : (DEFUN func (X) (COND (end-test end-value) (T (aug-fun aug-val) (func reduced-x)))) q Example1 : Func :COUNT-SLICES End-test : (NULL X) End-value :0 Aug-fun :+ Aug-val :1 Reduced-x :(REST X) (defun count-slices (x) (cond ((null x) 0) (t (+ 1 (count-slices (rest x))))))

KU NLP Artificial Intelligence23 Single-Test Augmenting Recursion(2/2) q Example2: The Factorial Function (defun fact (n) (cond ((zerop n) 1) (t (* n (fact (- n 1))))))

KU NLP Artificial Intelligence24 List-Consing Recursion (A Special Case of Augmenting Recursion) q Template : (DEFUN func (N) (COND (end-test NIL) (T (CONS new-element (func reduced-n))))) q Example : Func :LAUGH End-test : (ZEROP N) New-element :‘HA Reduced-n :(- N 1) (defun laugh (n) (cond ((zerop n) nil) (t (cons ‘ha (laugh (- n 1))))))

KU NLP Artificial Intelligence25 Simultaneous Recursion on Several Variables (Using the Single-Test Recursion Template) q Template : (DEFUN func (N X) (COND (end-test end-value) (T (func reduced-n reduced-x)))) q Example : Func :MY-NTH End-test : (ZEROP N) End-value : (FIRST X) Reduced-n :(- N 1) Reduced-x :(REST X) (defun my-nth (n x) (cond ((zerop n) (first X) (t (my-nth (- n 1) (rest x)))))

KU NLP Artificial Intelligence26 Conditional Augmentation (1/2) q Template (DEFUN func (X) (COND (end-test end-value) (aug-test (aug-fun aug-val (func reduced-x))) (T (func reduced-x)))) q Example Func :EXTRACT-SYMBOLS End-test : (NULL X) End-value : NIL Aug-test :(SYMBOLP (FIRST X)) Aug-fun :CONS Aug-val :(FIRST X) Reduced-x :(REST X)

KU NLP Artificial Intelligence27 Conditional Augmentation (2/2) (defun extract-symbols (X) (cond ((null x) nil) ((symbolp (first x)) (cons (first x) (extract-symbols (rest x)))) (t (extract-symbols (rest x)))))

KU NLP Artificial Intelligence28 Multiple Recursion(1/2) q Template : (DEFUN func (X) (COND (end-test1 end-value-1) (end-test2 end-value-2) (T (combiner (func first-reduced-n) (func second-reduced-n))))) q Example : Func :FIB End-test-1 : (EQUAL N 0) End-value-1 : 1 End-test-2 : (EQUAL N 1) End-value-2 : 1 Combiner :+ First-reduced-n : (- N 1) Second-reduced-n :(- N 2)

KU NLP Artificial Intelligence29 Multiple Recursion(2/2) (defun fib (n) (cond (equal n 0) 1) (equal n 1) 1) (t (+ (fib (- n 1)) (fib (- n 2))))))

KU NLP Artificial Intelligence30 CAR/CDR Recursion (A Special Case of Multiple Recursion)(1/2) q Template : (DEFUN func (X) (COND (end-test-1 end-value-1) (end-test-2 end-value-2) (T (combiner (func CAR X)) (func (CDR X)))))) q Example : Func :FIND-NUMBER End-test-1 : (NUMBERP X) End-value-1 : X End-test-2 : (ATOM X) End-value-2 : NIL Combiner :OR

KU NLP Artificial Intelligence31 CAR/CDR Recursion (A Special Case of Multiple Recursion)(2/2) (defun find-number (x) (cond ((numberp x) x) ((atom x) nil) (t (or (find-number (car x)) (find-number (cdr x))))))

KU NLP Artificial Intelligence32 연습문제 q VECTORPLUS X Y : Write a function (VECTORPLUS X Y) which takes two lists and adds each number at the same position of each list.  예 ) (VECTORPLUS ‘( ) ‘(8 5 2)) → ( ) q답)q답) (DEFUN VECPLUS (X Y) (COND ((NULL X) Y) ((NULL Y) X) (T (CONS (+ (CAR X) (CAR Y) (VECPLUS (CDR X) (CDR Y))))))