Download Lisp

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Ch 15. An Introduction to LISP
KU NLP
 15.0 Introduction
 15.1 LISP: A Brief Overview
 15.1.1 Symbolic Expressions, the Syntactic Basis of LISP
 15.1.2 Control of LISP Evaluation: quote and eval
 15.1.3 Programming in LISP: Creating New Functions
 15.1.4 Program Control in LISP: Conditionals and Predicates
 15.1.5 Functions, Lists, and Symbolic Computing
 15.1.6 Lists as Recursive Structures
 15.1.7 Nested Lists, Structure, and car/cdr Recursion
 15.1.8 Binding Variables Using set
 15.1.9 Defining Local Variables Using let
 15.1.10 Data Types in Common LISP
Artificial Intelligence
1
15.0 Characteristics of LISP
KU NLP
1.
Language for artificial intelligence programming
a.
2.
Imperative language
a.
b.
3.
Originally designed for symbolic computing
Describe how to perform an algorithm
Contrasts with declarative languages such as PROLOG
Functional programming
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
a.
4.
Popularity in the AI community
a.
b.
Widely used as a language for implementing AI tools and models
High-level functionality and rich development environment make it
an ideal language for building and testing prototype systems.
Artificial Intelligence
2
15.1 LISP: A Brief Overview
KU NLP
 Syntactic elements of LISP
 Symbolic expressions : S-expressions
 Atom : basic syntactic units
 List
 Both programs and data are represented as s-expressions
 Atom




Letters (upper, lower cases)
Numbers
Characters : * + - / @ $ % ^ & _ < > ~ .
Example
3.1416
 Hyphenated-name
 *some-global*
 nil


x
Artificial Intelligence
3
List and S-expression
KU NLP
 List
 A sequence of either atoms or other lists separated by
blanks and enclosed in parentheses.
 Example
 (1 2 3 4)
 (a (b c) (d (e f)))
 Empty list “( )” : nil
 nil is the only s-expression that is considered to be both an
atom and a list.
 S-expression
 An atom is an s-expression.
 If s1, s2,…, sn are s-expressions,
then so is the list (s1 s2 … sn).
Artificial Intelligence
4
Read-Eval-Print
KU NLP
 Interactive Environment
 User enters s-expressions
 LISP interpreter prints a prompt
 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.
 > (* 7 9)
63
Artificial Intelligence
5
Control of LISP Evaluation:
quote & eval
KU NLP
 quote : ‘
 prevent the evaluation of arguments
 (quote a) ==> a
 (quote (+ 1 3)) ==> (+ 1 3)
 ‘a ==> a
 ‘(+ 4 6) ==> (+ 4 6)
 eval
 allows programmers to evaluate s-expressions at will
 (eval (quote (+ 2 3))) ==> 5
 (list ‘* 2 5) ==> (* 2 5)
 (eval (list ‘* 2 5)) ==> 10
Artificial Intelligence
6
Programming in LISP:
Creating New Functions
KU NLP
 Syntax of Function Definition
 (defun <function-name> (<formal parameters>)
<function body>)
 defun : define function
 Example
 (defun square (x)
(* x x))
 (defun hypotenuse (x y)
(sqrt (+ (square x)
(square y))))
Artificial Intelligence
;the length of the hypotenuse is
;the square root of the sum of
;the square of the other sides.
7
Program Control in LISP:
Conditionals & Predicates(1/2)
KU NLP
 Conditions
 (cond (<condition 1> <action1>)
(<condition 2> <action 2>)
...
(<condition n> <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
 Predicates
 Example
(oddp 3)
 (minusp 6)
 (numberp 17)

Artificial Intelligence
; whether its argument is odd or not
8
Conditionals & Predicates(2/2)
KU NLP
 Alternative Conditions
 (if test action-then action-else)
 (defun absolute-value (x)
(if (< x 0) (- x) x))
 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
 Evaluate arguments, stopping when any one of arguments evaluates to
nil
 (or action1 ... action-n)
; disjunction
 Evaluate its arguments only until a non-nil value is encountered
Artificial Intelligence
9
List as recursive structures
KU NLP
 Cons cell: data structure to hold a list in LISP
 car - holds the first element.
 cdr - holds the rest in the list.
 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
 Constructing a list
 (cons ‘a ‘(b c)) ==> (a b c)
 (cons ‘a nil) ==> (a)
 (cons ‘(a b) ‘(c d)) ==> ((a b) c d)
Artificial Intelligence
10
Nested lists, structure,
car/cdr recursion
KU NLP
 More list construction
 (append ‘(a b) ‘(c d)) ==> (a b c d)
 (cons ‘(a b) ‘(c d)) ==> ((a b) c d)
 Counting the number of elements in the list
 (length ‘((1 2) 3 (1 (4 (5))))) ==> 3
Artificial Intelligence
11
Binding variables: set(1/2)
KU NLP
 (setq <symbol> <form>)
 bind <form> to <symbol>
 <symbol> is NOT evaluated.
 (set <place> <form>)
 replace s-expression at <place> with <form>
 <place> is evaluated. (it must exists.)
 (setf <place> <form>)
 generalized form of set: when <place> is a symbol, it behaves like setq;
otherwise, it behaves like set.
Artificial Intelligence
12
Binding variables: set(2/2)
KU NLP
 Examples of set / setq
 (setq x 1) ==> 1 ;;;
 (set a 2) ==> ERROR!!
 (set ‘a 2) ==> 2 ;;;
 (+ a x) ==> 3
 (setq l ‘(x y z)) ==>
 (set (car l) g) ==> g
 l ==> (g y z)
assigns 1 to x
;;; a is NOT defined
assigns 2 to a
(x y z)
 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)
Artificial Intelligence
13
Local variables: let(1/2)
KU NLP
 Consider a function to compute roots of a quadratic equation:
ax2+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-roots1 1 2 1) ==> (-1.0 -1.0)
 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)))))
Artificial Intelligence
14
Local variables: let(2/2)
KU NLP
 Any variables within let closure are NOT bound at top level.
 More improvement (binding values in local variables)
 (defun quad-roots3 (a b
(let ((temp (sqrt (- (*
((denom (*2 a)))
(list (/ (+ (- b)
(/ (- (- b)
Artificial Intelligence
c)
b b) (* 4 a c))))
temp) denom)
temp) denom))))
15
For Homework
KU NLP
 Representing predicate calculus with LISP
 ∀x likes (x, ice_cream)
(Forall (VAR X) (likes (VAR X) ice_cream))
 ∃x foo (x, two, (plus two three))
(Exist (VAR X) (foo (VAR X) two (plus two three)))
 Connectives
 ¬S
(nott S)
 S1 ∧ S2
(ANDD S1 S2)
 S1 ∨ S2
(ORR
S1 S2)
 S1 → S2
(imply
S1 S2)
 S1 ≡ S2
(equiv
S1 S2)
 Do not use the builtin predicate names.
Artificial Intelligence
16
For Homework
KU NLP
 숙제를 간략하게 하기 위해서 다음 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)
Artificial Intelligence
17
For Homework
KU NLP
(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)))
Artificial Intelligence
18
Recursion Templates
KU NLP
 Recursion template
 A few standard forms of recursive Lisp functions.
 You can create new functions by choosing a template and filling in
the blanks.
 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
Artificial Intelligence
19
Double-Test Tail Recursion
KU NLP
 Template :
(DEFUN func (X)
(COND (end-test-1 end-value-1)
(end-test-2 end-value-2)
(T (func reduced-x))))
 Example :
Func :
End-test-1 :
End-value-1 :
End-test-2 :
E nd-value-2 :
Reduced-x :
ANYODDP
(NULL X)
NIL
(ODDP(FIRST X))
T
(REST X)
(defun anyoddp (x)
(cond ((null x) nil)
((oddp (first x)) t)
((t (anyoddp (rest x))))
Artificial Intelligence
20
Single-Test Tail Recursion
KU NLP
 Template :
(DEFUN func (X)
(COND (end-test end-value)
(T (func reduced-x))))
 Example :
Func :
FIND-FIRST-ATOM
End-test :
(ATOM X)
End-value :
Reduced-x :
X
(FIRST X)
(defun find-first-atom (x)
(cond ((atom x) x)
((t (find-first-atom (first x))))
Artificial Intelligence
21
Single-Test Augmenting Recursion(1/2)
KU NLP
 Template :
(DEFUN func (X)
(COND (end-test end-value)
(T (aug-fun aug-val)
(func reduced-x))))
 Example1 :
Func :
COUNT-SLICES
End-test :
(NULL X)
End-value :
Aug-fun :
Aug-val :
Reduced-x :
0
+
1
(REST X)
(defun count-slices (x)
(cond ((null x) 0)
(t (+ 1 (count-slices (rest x))))))
Artificial Intelligence
22
Single-Test Augmenting Recursion(2/2)
KU NLP
 Example2: The Factorial Function
(defun fact (n)
(cond ((zerop n) 1)
(t (* n (fact (- n 1))))))
Artificial Intelligence
23
KU NLP
List-Consing Recursion
(A Special Case of Augmenting Recursion)
 Template :
(DEFUN func (N)
(COND (end-test NIL)
(T (CONS new-element
(func reduced-n)))))
 Example :
Func :
End-test :
New-element :
Reduced-n :
LAUGH
(ZEROP N)
‘HA
(- N 1)
(defun laugh (n)
(cond ((zerop n) nil)
(t (cons ‘ha (laugh (- n 1))))))
Artificial Intelligence
24
KU NLP
Simultaneous Recursion on Several Variables
(Using the Single-Test Recursion Template)
 Template :
(DEFUN func (N X)
(COND (end-test end-value)
(T (func reduced-n reduced-x))))
 Example :
Func :
End-test :
End-value :
Reduced-n :
Reduced-x :
MY-NTH
(ZEROP N)
(FIRST X)
(- N 1)
(REST X)
(defun my-nth (n x)
(cond ((zerop n) (first X)
(t (my-nth (- n 1) (rest x)))))
Artificial Intelligence
25
Conditional Augmentation (1/2)
KU NLP
 Template
(DEFUN func (X)
(COND (end-test end-value)
(aug-test (aug-fun aug-val
(func reduced-x)))
(T (func reduced-x))))
 Example
Func :
End-test :
End-value :
Aug-test :
Aug-fun :
Aug-val :
Reduced-x :
Artificial Intelligence
EXTRACT-SYMBOLS
(NULL X)
NIL
(SYMBOLP (FIRST X))
CONS
(FIRST X)
(REST X)
26
Conditional Augmentation (2/2)
KU NLP
(defun extract-symbols (X)
(cond ((null x) nil)
((symbolp (first x))
(cons (first x)
(extract-symbols (rest x))))
(t (extract-symbols (rest x)))))
Artificial Intelligence
27
Multiple Recursion(1/2)
KU NLP
 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)))))
 Example :
Func :
End-test-1 :
End-value-1 :
End-test-2 :
End-value-2 :
Combiner :
First-reduced-n :
Second-reduced-n :
Artificial Intelligence
FIB
(EQUAL N 0)
1
(EQUAL N 1)
1
+
(- N 1)
(- N 2)
28
Multiple Recursion(2/2)
KU NLP
(defun fib (n)
(cond (equal n 0) 1)
(equal n 1) 1)
(t (+ (fib (- n 1))
(fib (- n 2))))))
Artificial Intelligence
29
KU NLP
CAR/CDR Recursion
(A Special Case of Multiple Recursion)(1/2)
 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))))))
 Example :
Func :
End-test-1 :
End-value-1 :
End-test-2 :
End-value-2 :
Combiner :
Artificial Intelligence
FIND-NUMBER
(NUMBERP X)
X
(ATOM X)
NIL
OR
30
KU NLP
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))))))
Artificial Intelligence
31
연습문제
KU NLP
 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 ‘(3 6 9 10 4) ‘(8 5 2))
→ (11 11 11 10 4)
 답)
(DEFUN VECPLUS (X Y)
(COND
((NULL X) Y)
((NULL Y) X)
(T (CONS (+ (CAR X) (CAR Y)
(VECPLUS (CDR X) (CDR Y))))))
Artificial Intelligence
32
Related documents