artificial intelligence week 2 : search - lisp

32
Artificial Intelligence Week 2 : SEARCH - LISP 최 최 최 최최 http://www.cs.cmu.edu/~dst/LispBook/index.html 1

Upload: ulf

Post on 22-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

Artificial Intelligence Week 2 : SEARCH - LISP . 최 윤 정 참고 http://www.cs.cmu.edu/~dst/LispBook/index.html. 함수와 데이터. int add( int i , int j) { return i+j ; }. int divide( int i , int j) { return i /j ; } // 인자의 순서도 중요. 데이터 : 속성 , information - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Artificial Intelligence Week 2 : SEARCH - LISP

최 윤 정

참고http://www.cs.cmu.edu/~dst/LispBook/index.html

1

Page 2: Artificial Intelligence  Week 2 :   SEARCH -  LISP

함수와 데이터

데이터 : 속성 , information 수 (number), 단어 (word) , 이들의 리스트

interger, float, ratio( 비율 ), string , list

함수 : 기능 , 역할

입력된 데이터로 결과 (result) 를 만들어 내는 box

2

int add(int i, int j){

return i+j ;}

int divide(int i, int j){

return i/j ;} // 인자의 순서도 중요

Page 3: Artificial Intelligence  Week 2 :   SEARCH -  LISP

함수와 데이터

심볼 (symbol) : Lisp 의 데이터 타입중 하나

Sequence of letter , digits and special characters X, My-name, THREE-IS-3 Special symbols :

T : logical True, Yes NIL: logical False, No, Emptiness

술어 (Predicate) : 질문 - 답변 함수

결과가 True 면 T, False 면 Nil 을 return 기본 build-in 함수들

NUMBERP, SYMBOLP, EQUAL, ODDP,EVENP .. 등

3

FOUR : symbol4 : integer+4 : integer+ : symbol7-11 : symbol

Page 4: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Combination function +, add1, add2

4

Page 5: Artificial Intelligence  Week 2 :   SEARCH -  LISP

List Most versatile data type ( ) 로 둘러 쌓인 item 들의 묶음

A : symbol, (A) : list (a b c), (1 2 3), ( I Love You) 실제 pointer 를 사용한 cons cell 의 연결구조 (NIL 을 가리키는 cell 이 마지막 요소 )

(RED GREEN BLUE)

( (BLUE SKY) (GREEN GRASS) (BROWN EARTH))

5

Page 6: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Exercise

6

Page 7: Artificial Intelligence  Week 2 :   SEARCH -  LISP

List 함수 : LENGTH Length : Top-level 의 cons cell 개수

(A (B C) D) : A , ( B C) , D 3 (A (B C) (D E F) ) : 3 ( ) = NIL = empty list : 0 NIL : symbol 이면서 list

7

3

Page 8: Artificial Intelligence  Week 2 :   SEARCH -  LISP

EQUAL

(A (B C) D) 와 (A B (C D)) 는 다름 ! 길이와 내부구조 , 순서가 모두 같을 때만 같음 .

8

Page 9: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Exercise

9

Page 10: Artificial Intelligence  Week 2 :   SEARCH -  LISP

FIRST, SECOND, THIRD, and REST

10

Page 11: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Return a Pointer

11

Page 12: Artificial Intelligence  Week 2 :   SEARCH -  LISP

CAR and CDR

12

Combination

Page 13: Artificial Intelligence  Week 2 :   SEARCH -  LISP

13

Page 14: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Exercise

( (A B) (C D) (E F ))

14

>>(setq x '( (a b) ( c d ) (e f )))>>(car x) >>(car (car (cdr x))) C>>(cdr (car (cdr x))) (D)

Page 15: Artificial Intelligence  Week 2 :   SEARCH -  LISP

CONS : Create a new cons cell

15

Page 16: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Cons : Nonlist structure (A B C . D)

#1 = (#1# . A)

#1 = ( A B C . #1#)

16

Page 17: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Exercise

17

Page 18: Artificial Intelligence  Week 2 :   SEARCH -  LISP

18

Page 19: Artificial Intelligence  Week 2 :   SEARCH -  LISP

LIST : creating a list

19

Page 20: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Exercise

20

Page 21: Artificial Intelligence  Week 2 :   SEARCH -  LISP

EVAL notation (* 3 (+ 5 6) )

(Oddp ( + 1 6))

21

Page 22: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Define Function in EVAL Notation

( defun average ( x y) ( / ( + x y)

2.0) )

22

Page 23: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Symbols and Lists as Data Evaluating symbols

pi : 3.14159 , Build-in variable ! Eggplant : ERROR! , unassigned variable

Quote : ‘ (equal kim park) : ERROR! Unassigned variable. (equal ‘kim ‘park) : nil

23

Page 24: Artificial Intelligence  Week 2 :   SEARCH -  LISP

3-ways to make lists

1. ( cons ‘a ‘(b c)) : (a b c) 2. ( list ‘a ‘b ‘c) : (a b c)

3.

24

Page 25: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Exercise

25

Page 26: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Review Exercise

26

Don’t parenthesize variables in the argument list; don’t quote variables;

Page 27: Artificial Intelligence  Week 2 :   SEARCH -  LISP

LISP on Computer

27

Page 28: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Read-Eval-Print Loop Listener 에서

수정 : ctrl - G

28

Page 29: Artificial Intelligence  Week 2 :   SEARCH -  LISP

defun : define function

But , The most frequently occurring errors in LISP are parenthetical errors. It

is thus almost imperative to employ some sort of counting or pairing device to check parentheses every time that a function is changed.

— Elaine Gord, ‘‘Notes on the debugging of LISP programs,’’ 1964.

29

Page 30: Artificial Intelligence  Week 2 :   SEARCH -  LISP

Setf : assign a value to a variable > (setf x ’(a b c))(A B C)

> (setf y (cons ’d (cdr x)))(D B C)

> x (A B C)

> y (D B C)

30

Page 31: Artificial Intelligence  Week 2 :   SEARCH -  LISP

HEAD, TAIL, and LONG-LIST are all global variables.

31

Page 32: Artificial Intelligence  Week 2 :   SEARCH -  LISP

COND : if

32