n 9)(< n 40)) (princ #\X)(decf n 10)) (t (format t "cannot handle ~S~%" n)(setq n nil)) ) )"> n 9)(< n 40)) (princ #\X)(decf n 10)) (t (format t "cannot handle ~S~%" n)(setq n nil)) ) )">
Download presentation
Presentation is loading. Please wait.
Published byWinfred Austin Modified over 8 years ago
1
CSE 341 -- S. Tanimoto Pattern Matching 1 Pattern Matching Many Lisp programs are “data driven”. The program exams the data and then decides how to process it. Rule-based programming: the program consists of a collection of rules and a means for applying them.
2
CSE 341 -- S. Tanimoto Pattern Matching 2 Production System Form A rule of the form if [condition] then [action] is sometimes called a production rule. Lisp’s COND form provides a means to implement rule testing and application. ; Example structure: (COND (condition1 action1) (condition2 action2)... (conditionN actionN) )
3
CSE 341 -- S. Tanimoto Pattern Matching 3 A Lisp Production System for Translation into Roman Numerals (defun to-roman () (let ((n nil))(loop (cond ((null n) (setq n (read))) ((equal n 0) (terpri) (setq n nil)) ((and (numberp n)(> n 0)(< n 4)) (princ #\I)(decf n)) ((and (numberp n)(= n 4)) (format t "IV")(decf n 4)) ((and (numberp n)(< n 9)) (princ #\V)(decf n 5)) ((and (numberp n)(> n 9)(< n 40)) (princ #\X)(decf n 10)) (t (format t "cannot handle ~S~%" n)(setq n nil)) ) )
4
CSE 341 -- S. Tanimoto Pattern Matching 4 Focus on the Condition ((equal n 0)) (terpri) (setq n nil)) A test to see whether the data possesses some particular property. We generally use predicates -- functions that return boolean values. Many conditions can be expressed as a comparison between the input data and a “pattern”. A trivial case is: (equal n 0); n = data, 0 = pattern.
5
CSE 341 -- S. Tanimoto Pattern Matching 5 The MATCH Function It’s not built into Common Lisp but defined in the text. It supports... 1. exact matching of 1-level lists (match '(hello there) my-input) 2. wild-card construct, e.g., (match '(my name is (? X)) '(my name is john) ) 3. wild-card with restrictive predicate, e.g., (match '(age (numberp age) '(age 21))) 4. wild-sequence construct, e.g., (* LST) (match '(from (* x) to (* y)) '(from jim and jan to manuel and tran))
6
CSE 341 -- S. Tanimoto Pattern Matching 6 What MATCH Returns MATCH returns an association list. (match '(hello there) '(hello)) NIL (match '(hello there) '(hello there)) ((YES. YES)) (match '(my name is (? X)) '(my name is john)) ((X. JOHN)(YES. YES))
7
CSE 341 -- S. Tanimoto Pattern Matching 7 What MATCH Returns (Cont.) (match '(age (numberp age) '(age 21) )) ((AGE. 21)(YES. YES)) (match '(from (* x) to (* y)) '(from jim and jan to manuel and tran) ) ((X JIM AND JAN) (Y MANUEL AND TRAN) (YES. YES))
8
CSE 341 -- S. Tanimoto Pattern Matching 8 Accessing Associations Returned by MATCH (match '(hello there) '(hello there)) ((YES. YES)) ;;; VAL is defined in MATCH.CL (val 'x '((x. 5)(y. 10)) 5 (val 'x (match '(hi (? x))'(hi there))) THERE
9
CSE 341 -- S. Tanimoto Pattern Matching 9 Accessing Associations (Cont.) (match '((* STUFF) is (? NAME)) '(my name is john)) ((STUFF MY NAME)(NAME. JOHN)(YES. YES)) (setq result *) ((STUFF MY NAME)(NAME. JOHN)(YES. YES)) (val 'stuff result) (MY NAME) (val ’name result) JOHN (val 'y result) NIL
10
CSE 341 -- S. Tanimoto Pattern Matching 10 The MATCH Function (continued) MATCH is defined in the file MATCH.CL You must load this file explicitly to use it. (load "MATCH.CL") or, if you have renamed the file to match.lsp, use (load "match.lsp") Make sure there is a copy of the file in your current directory.
11
CSE 341 -- S. Tanimoto Pattern Matching 11 Getting User Input (READ) returns the next form from standard input. (READ-LINE) returns a string containing the next line of text from standard input. To get a line of text as a list, we can use: (defun get-input-as-list (prompt-string) (format t "~A~%" prompt-string) (read-line) ; Swallow & ignore current line. (read-from-string ;Get a form from a string. (concatenate ;Combine some sequences, 'string ;result to be a string. "(" ;begin a list (read-line) ;words in the list ")" ;end of the list ) )
12
CSE 341 -- S. Tanimoto Pattern Matching 12 Using GET-INPUT-AS-LIST (get-input-as-list "How are you?") How are you? I am fine (I AM FINE) 11 READ-FROM-STRING returns 2 values -- the form read and the number of characters used from the string. Therefore GET-INPUT-AS-LIST also returns 2 values.
13
CSE 341 -- S. Tanimoto Pattern Matching 13 Using MATCH to Parse Input (load "MATCH.CL") (defun get-favorite-color () (let* ((ans (get-input-as-list "What is your favorite color?") ) (result (or (match '((? c)) ans) (match '(its (? c)) ans) (match '((? c) is my (* x)) ans) (match '(my (* x) is (? c)) ans) ) ) ) (if result (val 'c result) 'unknown) ) )
14
CSE 341 -- S. Tanimoto Pattern Matching 14 Parsing Input with MATCH (Cont.) >(get-favorite-color) What is your favorite color? Its red RED >(get-favorite-color) What is your favorite color? Green is my favorite color GREEN >(get-favorite-color) What is your favorite color? black BLACK >(get-favorite-color) What is your favorite color? My very favorite is chartreuse CHARTREUSE
15
CSE 341 -- S. Tanimoto Pattern Matching 15 Reading Math Formulas as English (load "MATCH.CL") (defun get-sum () (let* ((exp (get-input-as-list "Describe a particular sum") ) (result (or (match '((? a) plus (? b)) exp) (match '(the sum of (? a) and (? b)) exp) ) ) ) (if result (list '+ (val 'a result)(val 'b result)) ) ) )
16
CSE 341 -- S. Tanimoto Pattern Matching 16 Reading Math Formulas (Cont.) >(get-sum) Describe a particular sum 5 plus 10 (+ 5 10) >(get-sum) Describe a particular sum the sum of area and perimeter (+ AREA PERIMETER)
17
CSE 341 -- S. Tanimoto Pattern Matching 17 The SHRINK: A Lisp Program for Pattern-directed Conversation Holds a conversation with the user. Illustrates how shallow a program’s knowledge can be and still present an image of understanding. Demonstrates key ideas from J. Weizenbaum’s ELIZA program, published in 1966.
18
CSE 341 -- S. Tanimoto Pattern Matching 18 Mechanisms in SHRINK Processing is oriented towards maintaining an illusion of understanding. Pattern matching used to “parse” user input. The YOU-ME-MAP function transposes first and second person references. Keyword-based rules respond to certain topics such as dreams, love, being positive or negative. Questions to the SHRINK are transformed into questions to the user. When all other rules fail, PUNT responses are used.
19
CSE 341 -- S. Tanimoto Pattern Matching 19 Uses Pattern Matching Sample rules ((match '(you feel (* x)) s) (format t "I SOMETIMES FEEL THE SAME WAY.~%") ) ((match '(because (* x)) s) (format t "IS THAT REALLY THE REASON.~%") ) ((setq b (match '(you have (* x)) s)) (print-q (append '(how long have you had) (val 'x b) )) )
20
CSE 341 -- S. Tanimoto Pattern Matching 20 A Sample Dialog with SHRINK (shrink) WELCOME TO MY SOFA! PLEASE ENCLOSE YOUR INPUT IN PARENTHESES. (hi there) TELL ME MORE (i have a little problem) HOW LONG HAVE YOU HAD A LITTLE PROBLEM? (ever since i told my girlfriend that i love computers more than her) ALL IS FAIR IN LOVE AND WAR (thanks maybe i should have told her that) BE MORE DECISIVE (ok) I SEE (bye) GOODBYE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.