Presentation is loading. Please wait.

Presentation is loading. Please wait.

Common LISP VS JAVA 김민철. Table of contents  1. LISP …………………………………………… 3  2. 데이터 타입 …………………………………… 4  3. 산술 및 논리 연산 ……………………………… 8  4. 변수 선언 및 할당 ………………………………

Similar presentations


Presentation on theme: "Common LISP VS JAVA 김민철. Table of contents  1. LISP …………………………………………… 3  2. 데이터 타입 …………………………………… 4  3. 산술 및 논리 연산 ……………………………… 8  4. 변수 선언 및 할당 ………………………………"— Presentation transcript:

1 Common LISP VS JAVA 김민철

2 Table of contents  1. LISP …………………………………………… 3  2. 데이터 타입 …………………………………… 4  3. 산술 및 논리 연산 ……………………………… 8  4. 변수 선언 및 할당 ……………………………… 14  5. Block ……………………………………………18  6. 조건문 …………………………………………… 20  7. 반복문 …………………………………………… 22  8. LIST 관련 함수 ………………………………… 24  9. 함수 ……………………………………………… 30  10. 기타 데이터 타입 ……………………………… 35  11. 패키지 ………………………………………… 38 2

3 1. LISP  LISP (LISt Processor)  LISt Processor 와 동의어로서 심볼 (Symbol) 들로 구성 된 리스트 (List) 들을 쉽게 조작할 수 있는 언어  Common LISP  LISP 의 많은 방언 (MacLISP, Inter LISP, Franz LISP) 들의 표준화의 결과로 나옴 3

4 2. 데이터 타입  LISP 데이터 타입  아톰 (Atom) 과 리스트 (List) 두 종류의 대상을 다룸  아톰은 숫자나 사람, 사물, 개념 등과 같은 대상을 표현 할 때 사용되는 심볼 (Symbol)  리스트는 괄호로 둘러싸여 있는 원소들로 구성  리스트의 원소는 원소는 아톰이거나 리스트로 되어 있음 4

5 2. 데이터 타입  Symbol 의 내부표현  Name : 심볼을 지칭하는 이름  Value Cell : 심볼에 의해 이름이 붙여진 변수의 값  Function : 정의된 함수를 가리키는 포인터  Property list : 심볼에 대한 기타 정보  Package : 서로 다른 모듈간의 생길 수 있는 혼란을 방지해 주는 정보 5 name Value Cell function Property list package

6 2. 데이터 타입  True and False  nil 은 false, 다른 모든 것은 true  0, “”true  () 은 nil, 따라서 () 는 false  list 는 list 를 만드는 함수, 인자가 없으므로 () 생성 6 Common LISP 결과 (if nil "yes" "no")no (if () "yes" "no")no (if '() "yes" "no")no (if (list) "yes" "no")no

7 2. 데이터 타입  t 는 true  Common LISP 은 Boolean datatype 미존재  Java 는 Boolean datatype 존재 7 Common LISP 결과 (if t "yes" "no")yes (if 0 "yes" "no")yes (if "" "yes" "no")yes (if [] "yes" "no")yes (and t nil)nil (or t nil)t

8 3. 산술 및 논리 연산  Arithmetic Function 8 Common LISPJAVA 결과 (+ 4 5 1)4 + 5 + 110 (- 9 2)9 – 27 (- 9 2 3)9 – 2 – 34 (* 2 3)2 * 36 (* 2 3 2)2 * 3 * 212 (/ 7 2)7 / 23 (/ 7 2.0)7 / 2.03.5 (% 7 4)7 % 43 (expt 2 3)Math.power(2, 3)8

9 3. 산술 및 논리 연산 9  Arithmetic Type check function  ~p 의 p 는 predicate 를 의미  T 는 TRUE, NIL 은 FALSE 를 의미  JAVA 에는 instanceof 연산자 존재  JavaScript 에 isNaN() 함수 존재 Common LISP 결과 (integerp 3.)T (floatp 3.)NIL (floatp 3.0)T

10 3. 산술 및 논리 연산  Converting String and Numbers 10 Common LISP 결과 JAVA 결과 (parse-integer "42")42 ; 2 Integer.parseInt(“42”)42 (read-from-string “42”)42 ; 2 (write-to-string 42)“42”String.valueOf(42)“42” Common LISP 결과 (parse-integer "42" :start 1)2 ; 2 (parse-integer "42" :end 1)4 ; 1 (parse-integer "42" :radix 8)34 ; 2

11 3. 산술 및 논리 연산  Comparing Strings 11 Common LISP 결과 JAVA 결과 (string= "Marx" "Marx")T "Marx".equals("Marx") Ture (string= "Marx" "marx")NIL "Marx".equals("marx") False (string-equal "Marx" "marx")T "Marx".equalsIgnoreCase(" marx") true (string< “A" “B”)0 "A".compareTo("B") (string< “a" “B”)NIL“a".compareTo("B")31 (string-lessp “A" “B")0 "a".compareToIgnoreCase(" B") (mismatch “AA" “BB" :from-end t)2

12 3. 산술 및 논리 연산  Comparison Functions 12 Common LISP 결과 JAVA 결과 (< 3 4)T3 < 4true (> 3 4)nil3 > 4false (<= 3 4)T3 <= 4true (>= 3 4)nil3 >= 4false (= 3 3)T3 == 3true (= 3 3.0)T3 == 3.0true (/= 3 4)T3 != 4true

13 3. 산술 및 논리 연산  equal datatype 까지 비교  /= 는 숫자만 비교 가능 13 Common LISP 결과 (equal "abc" "abc")t (equal 3 3)t (equal 3.0 3.0)t (equal 3 3.0)nil (equal '(3 4 5) '(3 4 5))t (equal '(3 4 5) '(3 4 "5"))nil (equal 'abc 'abc)t (not (= 3 4))t (/= 3 4)t (not (equal 3 4))t

14 4. 변수 선언 및 할당  Global and Local Variables  setq : 변수에 값을 대입하는 함수  let : local 변수 선언 함수  let 을 사용 안하면 전역변수 14 Common LISPJAVA (setq x 1)public static int x = 1; (setq a 3 b 2 c 7)public static int a = 3, b = 2, c = 7; ( let (a b) (setq a 3) (setq b 4) (+ a b) ){ int a = 3, b = 4; return a + b; } ( let ((a 3) (b 4)) (+ a b) )

15 4. 변수 선언 및 할당 15 Common LISP 동작 @ (setq a 1 b a c b) a b c 모두 1 @ (setq w 77) > 77 @ (let ((w 8) (x w)) (+ w x)) > 85 Let 함수로 변수 선언 한번에 처리 <= 이 위치의 w 가 77 따라서 결과 85 @ (setq w 77) > 77 @ (let* ((w 8) (x w)) (+ w x)) > 16 Let* 는 순서대로 처리 <= 이 위치의 w 8

16 4. 변수 선언 및 할당 16  Quote 함수는 인수를 평가하지 않고 그 자체를 값으로 함  약칭표기 ‘ 를 제공함 Common LISP 동작 @ (setq b 23) > 23 @ (setq a b) > 23 @ a > 23 <= a 에 b 의 값 23 이 할당 @ (setq b 23) > 23 @ (setq a (quote b)) > B @ a > B <= a 에 b 가 할당

17 4. 변수 선언 및 할당 17  값은 하나 이상의 이름을 지닐 수 있음  LISP 도 JAVA 와 같이 포인터 노출하지 않음 Common LISP 동작 JAVA 동작 @ (setq L1 (list 'a 'b 'c)) > (A B C) @ (setq L2 L1) > (A B C) @ (eq L1 L2) > T @ (setq L3 (list 'a 'b 'c)) > (A B C) @ (eq L3 L1) > NIL <= L2 가 L1 을 가리킴 ( 동일한 데 이터 ) <= L3 가 새 로운 리스 트를 가리 킴 List L1 = new ArrayList(); L1.add(“a”); L1.add(“b”); L1.add(“c”); List L2 = L1; System.out.println(L1 == L2); List c = new ArrayList(); L3.add(“a”); L3.add(“b”); L3.add(“c”); System.out.println(L3 == L1); <= 동일 데이터 참조 <= 다른 데이터 참조

18 5. Block  Block Expressions 18 Common LISPJAVA (progn (princ "hi") (princ "lo") ) { System.out.print(“hi”); System.out.print(“lo”); } (setq a 987) ;; a = 987 (let ((a 1)) ;; a = 1... ) ;; a = 987 int a = 987; // a= 987; { int a = 1; // a = 1; } // a = 987;

19 5. Block Common LISPJAVA (let ((a 1) (b 9)) ;; a=1 ;; b = 9 (let ((a 2)) ;; a = 2 ;; b= 9 (let ((a 3)) ;; a = 3 ;; b = 9... ) ;; a = 2, b= 9 ) ;; a = 1, b = 9 ) { int a = 1, b = 9; // a = 1; b = 9; { int a = 2; // a = 2; b = 9; { int a = 3; // a = 3; b = 9; } // a = 2; b = 9; } // a = 1; b = 9; } 19

20 6. 조건문  If then else 20 Common LISPJAVA (if (> 3 2) (princ "yes") )if(3 > 2) System.out.print(“yes”); (if (< 3 2) (princ "yes") (princ "no") ) if(3 < 2) System.out.print(“yes”); else System.out.print(“no”); (if nil (princ "yes") (princ "no") ) if(false) System.out.print(“yes”); else System.out.print(“no”); (if (< 2 3) (progn ; true … ) (progn ; false … ) if(2 < 3) { … } else { … }

21 6. 조건문  COND 21 Common LISP 결과 (let ((a 1) (b 2) (c 1) (d 1)) (cond ((eql a b) 1) ((eql a c) "First form" 2) ((eql a d) 3) ) 2 <= 조건이 T 인 form 의 마지막 실행 (let ((a 32)) (cond ((eql a 13) "An unlucky number") ((eql a 99) "A lucky number") (t "Nothing special about this number") ) Nothing special about this number <= 위의 조건들에 맞 지 않고 마지막 조건 이 T 이므로 실행

22 7. 반복문  반복문 22 Common LISPJAVA (setq x 0) (while (< x 4) (princ x) (setq x (1+ x) ) public static int x = 32; …. while(x < 4) { System.out.print(x); x++; } (let ((x 32)) (while (< x 127) (princ x) (setq x (+ x 1)) ) int x = 32; while(x < 127) { System.out.print(x); x = x + 1; }

23 7. 반복문 23 Common LISPJAVA (dotimes (i 5) (princ i) ) > 01234 for(int i=0; i<5; i++) System.out.print(i); (dolist (x ‘(1 2 3)) (print x) ) >123 >NIL int[] arr = new int[3]; arr[0] = 1; arr[1] = 2; arr[2] = 3; for (int x: arr) { System.out.print(x); } (do ((i 0 (1+ i))) ((>= i < 4)) (princ i) ) >0123 >NIL for(int i=0; i<4; i++) System.out.print(i);

24 8. LIST 관련 함수  CAR  LIST 의 첫 번째 요소를 꺼냄  FIRST 와 동일 함수 24 Common LISP 결과 (CAR ‘(A B C))A (CAR ‘((A B) (C D)))(A B) (CAR ‘(PLUS 2 3))PLUS (CAR ‘(4 8 5))4

25 8. LIST 관련 함수  CDR  첫 번째 요소를 제외한 나머지 요소를 꺼냄  REST 와 동일 함수 25 Common LISP 결과 (CDR ‘(A B C))(B C) (CDR ‘((A B) (C D)))(C D) (CDR ‘(PLUS 2 3))(2 3) (CDR ‘(4 8 5))(8 5) (CDR ‘(A))NIL

26 8. LIST 관련 함수  CxxxR  C 와 R 사이에 있는 A 와 D 의 개수와 순번에 따라 CAR 과 CDR 을 조합  조합의 개수는 시스템에 따라 다르나, 보통 3 개까지 가능 26 Common LISP 결과 (CADR ‘(A B C))B (CDAR ‘((A B C) D E))(B C) (CADDR ‘(A (B C) (D E)))(D E)

27 8. LIST 관련 함수  CONS  두 인수를 받아 두 번째 LIST 에 첫번째 인자를 포함  LIST  여러 요소를 받아 LIST 를 만듬 27 Common LISP 결과 (CONS ‘A ‘(B C))(A B C) (CONS ‘(A B) ‘(C D))((A B) C D) (CONS ‘A NIL)(A) Common LISP 결과 (LIST ‘A ‘B ‘C)(A B C) (LIST ‘(A) ‘(C D))(A (C D)) (LIST ‘A)(A)

28 8. LIST 관련 함수  APPEND  두 인수를 받아 1 개의 리스트로 만 듬  REVERSE  리스트 요소의 순번을 반대로 만듬 28 Common LISP 결과 (APPEND ‘(A B) ‘(C D))(A B C D) (APPEND ‘(A) ‘((B C) D))(A (B C) D) (APPEND ‘A NIL)(A) Common LISP 결과 (REVERSE ‘A ‘(B C))(C B A) (REVERSE ‘((A B) (C D))((C D) (A B)) (REVERSE ‘(A (B C D))((B C D) A)

29 8. LIST 관련 함수  ASSOC  두 번째 인수의 첫 번째 요소부터 첫 번째 요소가 첫 번 째 인수와 일치하는 것을 찾음 29 Common LISP 결과 (ASSOC ‘A ‘((X Y) (A B) (S Q)))(A B) (ASSOC ‘X ‘((A B) (C D) (Y X)))NIL

30 9. 함수  함수 정의 및 호출 30 Common LISP @ (defun secret-number (the-number) (let ((the-secret 37)) (cond ((= the-number the-secret) 'that-is-the-secret-number) ((< the-number the-secret) 'too-low) ((> the-number the-secret) 'too-high)))) > SECRET-NUMBER @ (secret-number 11) > TOO-LOW @ (secret-number 99) > TOO-HIGH @ (secret-number 37) > THAT-IS-THE-SECRET-NUMBER JAVA String secretNumber(int number) { int secret = 37; if(number == secret) return “that-is-the-secret-number”; if(number < secret) return “too-low”; if(number > secret) return “too-high”; } secretNumber(11); // “too-low” secretNumber(99); // “too-high” secretNumber(37); // “that-is-the-secret-number”

31 9. 함수 31 CommonLISPJAVA @ (defun my-calc (a b c x) (+ (* a (* x x)) (* b x) c) ) > MY-CALC @ (my-calc 3 2 7 5) > 92 int myCalc (int a, int b, int c, int x) { return (a * (x * x)) + (b * x) + c; } myCalc(3, 2, 7, 5); // 92

32 9. 함수  LAMBDA 함수  한 곳에서만 사용되어 이름이 필요없는 함수  JavaScript 에 unnamed function 존재  Obj.onclick = function() { … } 32 Common LISP 동작 @ (lambda (a b c x) (+ (* a (* x x)) (* b x) c)) > Error @ ((lambda (a b c x) (+ (* a (* x x)) (* b x) c)) 3 2 7 5) > 92 <= lambda 함수 정의 <= lambda 함수 호출

33 9. 함수  인수전달 및 반환 33 CommonLISP 동작 @ (multiple-value-bind (a b c) (values 2 3 5 'x 'y) (+ a b c)) > 10 초과된 인수 무시 @ (multiple-value-bind (w x y z) (values :left :right) (list w x y z)) > (:LEFT :RIGHT NIL NIL) 부족한 값은 NIL 로 binding @ (defun foo (p q) (values (list :p p) (list :q q))) > FOO @ (foo 5 6) > (:P 5) > (:Q 6) 2 개 이상의 list 반환

34 9. 함수  재귀호출 34 Common LISPJAVA @ (defun factorial (n) (cond ((= n 0) 1) (t (* n (factorial (- n 1)))))) > FACTORIAL @ (factorial 10) > 120 public int factorial(int n) { if(n == 0) return 1; else return n * factorial(n-1); } … Factorial(5);

35 10. 기타 데이터 타입  구조체 (Common LISP) 35 Common LISP 동작 @ (defstruct struct-1 color size shape position weight) > STRUCT-1 @ (setq object-2 (make-struct-1 :size 'small :color 'green :weight 10 :shape 'square)) > #S(STRUCT-1 :COLOR GREEN :SIZE SM ALL :SHAPE SQUARE :POSITION NIL :WEIGHT 10) @ (struct-1-shape object-2) > SQUARE @ (struct-1-position object-2) > NIL @ (setf (struct-1-position object-2) '(under tab le)) > (UNDER TABLE) @ (struct-1-position object-2) > (UNDER-TABLE) struct-1 구조체 정의 (make-structname 과 st ructname-slotname 함수를 생성 ) struct-1 구조체 생성, object-2 에 할당 object-2 의 shape 가져옴 object-2 의 position 가져옴 object-2 의 position 설정 object-2 의 position 가져옴

36 10. 기타 데이터 타입  클래스 (JAVA) 36 JAVA public class Struct1 { private String color; private String size; private String shape; private String position; private int weight; public Struct1() { } public Struct1(String color, String size, String shape, int weight) { super(); this.color = color; this.size = size; this.shape = shape; this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } … } …. Struct1 object2 = new Struct1(“green”, “small”, ”square”, 10); Object2.getShape(); Object2.getPosition(); Object2.setPosition(“under table”); Object2.getPosition();

37 10. 기타 데이터 타입  Hash Table 37 Common LISP 동작 @ (setq ht1 (make-hash-table)) > # @ (gethash 'quux ht1) > NIL @ (setf (gethash 'baz ht1) 'baz-value) > BAZ-VALUE @ (gethash 'baz ht1) > BAZ-VALUE > T @ (setf (gethash 'gronk ht1) nil) > NIL @ (gethash 'gronk ht1) > NIL > T Hash table 생성 및 할당 ht1 에서 gethash 로 키가 ‘quux 에 대한 값에 접근 Ht1 에 ‘baz 를 키로 ‘baz-value 설 정 Ht1 에 키가 ‘baz 인 값 접근 ht1 에 ‘gronk 를 키로 nil 설정 키와 관련있는 값 찾으면 T, 아니면 NIL

38 11. 패키지  패키지  이름 충돌을 방지하기 위해 사용 38 Common LISP ;;;; ---- File 1 ---- (defpackage util1 (:export init func1 func2) (:use common-lisp)) (in-package util1) (defun init () 'util1-init) (defun func1 () 'util1-func1) (defun func2 () 'util1-func2) ;;;; ---- File 2 ---- (defpackage util2 (:export init func1 func2) (:use common-lisp)) (in-package util2) (defun init () 'util2-init) (defun func1 () 'util2-func1) (defun func2 () 'util2-func2) ;;;; ---- File 3 ---- (defpackage client (:use common-lisp) (:import-from util1 func1) (:import-from util2 func2)) (in-package client) (defun init () 'client-init) (util1:init) (util2:init) (init) (func1) (func2)

39 Q & A 39


Download ppt "Common LISP VS JAVA 김민철. Table of contents  1. LISP …………………………………………… 3  2. 데이터 타입 …………………………………… 4  3. 산술 및 논리 연산 ……………………………… 8  4. 변수 선언 및 할당 ………………………………"

Similar presentations


Ads by Google