![Page 1: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/1.jpg)
SNU 4190.210 c©Kwangkeun Yi
SNU 4190.210 áÔ�ÐÕªA�bç
"é¶o�(Principles of Programming)
Part III
Prof. Chung-Kil Hur
![Page 2: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/2.jpg)
SNU 4190.210 c©Kwangkeun Yi
�YV
1 °úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs imperativeprogramming)
2 áÔ�ÐÕªÏþ�_� s�K�: 8��âõ� Bj�o�(environment &memory)
![Page 3: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/3.jpg)
SNU 4190.210 c©Kwangkeun Yi
��6£§
1 °úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs imperativeprogramming)
2 áÔ�ÐÕªÏþ�_� s�K�: 8��âõ� Bj�o�(environment &memory)
![Page 4: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/4.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)�ª�AᤠáÔ�ÐÕªA�bç ~½Ód��\� 0pxnqK���
I °úכ: ��� �t� ·ú§��H��(value, immutable)
I (+ 2 1)��H 3�¦ _�p�; 2�� ���K�"f 3s� ÷&��H ��sכ ��_��.I (add-element 1 S)��H S ∪ {1}��� |9�½+Ë�¦ _�p�.I S\� 1 s� '�����)a, ����o�)a S\�¦ _�p� �t� ·ú§6£§.I t��FK��t�_� áÔ�ÐÕªA�bç ~½Ód��(�©�0A_�. úo��7HÕüt ~½Ód��)
![Page 5: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/5.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)�ª�AᤠáÔ�ÐÕªA�bç ~½Ód��\� 0pxnqK���
I °úכ: ��� �t� ·ú§��H��(value, immutable)I (+ 2 1)��H 3�¦ _�p�; 2�� ���K�"f 3s� ÷&��H ��sכ ��_��.
I (add-element 1 S)��H S ∪ {1}��� |9�½+Ë�¦ _�p�.I S\� 1 s� '�����)a, ����o�)a S\�¦ _�p� �t� ·ú§6£§.I t��FK��t�_� áÔ�ÐÕªA�bç ~½Ód��(�©�0A_�. úo��7HÕüt ~½Ód��)
![Page 6: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/6.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)�ª�AᤠáÔ�ÐÕªA�bç ~½Ód��\� 0pxnqK���
I °úכ: ��� �t� ·ú§��H��(value, immutable)I (+ 2 1)��H 3�¦ _�p�; 2�� ���K�"f 3s� ÷&��H ��sכ ��_��.I (add-element 1 S)��H S ∪ {1}��� |9�½+Ë�¦ _�p�.I S\� 1 s� '�����)a, ����o�)a S\�¦ _�p� �t� ·ú§6£§.
I t��FK��t�_� áÔ�ÐÕªA�bç ~½Ód��(�©�0A_�. úo��7HÕüt ~½Ód��)
![Page 7: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/7.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)�ª�AᤠáÔ�ÐÕªA�bç ~½Ód��\� 0pxnqK���
I °úכ: ��� �t� ·ú§��H��(value, immutable)I (+ 2 1)��H 3�¦ _�p�; 2�� ���K�"f 3s� ÷&��H ��sכ ��_��.I (add-element 1 S)��H S ∪ {1}��� |9�½+Ë�¦ _�p�.I S\� 1 s� '�����)a, ����o�)a S\�¦ _�p� �t� ·ú§6£§.I t��FK��t�_� áÔ�ÐÕªA�bç ~½Ód��(�©�0A_�. úo��7HÕüt ~½Ód��)
![Page 8: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/8.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)I Óüt|�: �©�I��� ���ô�Ç��(state, mutable)
I (add-element S 1)��H |9�½+Ë S�� ���K�"f 1 s� '�����)aDh�Ðî�r |9�½+Ës� H�d.
I Óüt|�_� �©�I�\�¦ ����or�v���H, s���� áÔ�ÐÕªÏþ�s� ¹ô�Çכ��9�âĺ� ú§��. (\V: 1-X<s��� n-½�&³�¦ [�v~�� “�<Êú_�s��¦”)
I ��� �Ù¼�Ð, í�H"f�� ×�æ¹כ.I Óüt|�_� �©�I�\�¦ ����or�v���H "î§î+þA áÔ�ÐÕªA�bç ~½Ód��
![Page 9: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/9.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)I Óüt|�: �©�I��� ���ô�Ç��(state, mutable)
I (add-element S 1)��H |9�½+Ë S�� ���K�"f 1 s� '�����)aDh�Ðî�r |9�½+Ës� H�d.
I Óüt|�_� �©�I�\�¦ ����or�v���H, s���� áÔ�ÐÕªÏþ�s� ¹ô�Çכ��9�âĺ� ú§��. (\V: 1-X<s��� n-½�&³�¦ [�v~�� “�<Êú_�s��¦”)
I ��� �Ù¼�Ð, í�H"f�� ×�æ¹כ.I Óüt|�_� �©�I�\�¦ ����or�v���H "î§î+þA áÔ�ÐÕªA�bç ~½Ód��
![Page 10: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/10.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)I Óüt|�: �©�I��� ���ô�Ç��(state, mutable)
I (add-element S 1)��H |9�½+Ë S�� ���K�"f 1 s� '�����)aDh�Ðî�r |9�½+Ës� H�d.
I Óüt|�_� �©�I�\�¦ ����or�v���H, s���� áÔ�ÐÕªÏþ�s� ¹ô�Çכ��9�âĺ� ú§��. (\V: 1-X<s��� n-½�&³�¦ [�v~�� “�<Êú_�s��¦”)
I ��� �Ù¼�Ð, í�H"f�� ×�æ¹כ.I Óüt|�_� �©�I�\�¦ ����or�v���H "î§î+þA áÔ�ÐÕªA�bç ~½Ód��
![Page 11: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/11.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)I Óüt|�: �©�I��� ���ô�Ç��(state, mutable)
I (add-element S 1)��H |9�½+Ë S�� ���K�"f 1 s� '�����)aDh�Ðî�r |9�½+Ës� H�d.
I Óüt|�_� �©�I�\�¦ ����or�v���H, s���� áÔ�ÐÕªÏþ�s� ¹ô�Çכ��9�âĺ� ú§��. (\V: 1-X<s��� n-½�&³�¦ [�v~�� “�<Êú_�s��¦”)
I ��� �Ù¼�Ð, í�H"f�� ×�æ¹כ.
I Óüt|�_� �©�I�\�¦ ����or�v���H "î§î+þA áÔ�ÐÕªA�bç ~½Ód��
![Page 12: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/12.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)I Óüt|�: �©�I��� ���ô�Ç��(state, mutable)
I (add-element S 1)��H |9�½+Ë S�� ���K�"f 1 s� '�����)aDh�Ðî�r |9�½+Ës� H�d.
I Óüt|�_� �©�I�\�¦ ����or�v���H, s���� áÔ�ÐÕªÏþ�s� ¹ô�Çכ��9�âĺ� ú§��. (\V: 1-X<s��� n-½�&³�¦ [�v~�� “�<Êú_�s��¦”)
I ��� �Ù¼�Ð, í�H"f�� ×�æ¹כ.I Óüt|�_� �©�I�\�¦ ����or�v���H "î§î+þA áÔ�ÐÕªA�bç ~½Ód��
![Page 13: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/13.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)\V: (append l r) �<Êú
I °úכ×�æd��(value, immutable): lõ� r�Ér ��� �t� ·ú§6£§.
(define (append l r)
(cond ((null? l) r)
((null? r) l)
(else (cons (car l) (append (cdr l) r)))
))
I Óüt|�×�æd��(state, mutable): ls� ���K�"f l@rs� H�d.
(define (append l r)
(cond ((null? l) (begin (change l r) l))
((null? r) l)
(else (begin (change (cdr l) (append (cdr l) r)) l))
))
![Page 14: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/14.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)áÔ�ÐÕªA�bç ���#Q[þt�Ér @/>h
I ¿º��t� ~½Ód���¦ �¿º ½��½+É Ãº e����H ~½Óîß��¦ ]j/BNI éß�, l��:rܼ�Ð t�"é¶ ���H ~½Ód��s� e���¦, "é¶ô�Ç����� ���Ér~½Ód��� ��0px
I Scheme: °úכ×�æd�� > Óüt|�×�æd��I ML: °úכ×�æd�� > Óüt|�×�æd��I Java: Óüt|�×�æd�� > °úכ×�æd��I C: Óüt|�×�æd�� > °úכ×�æd��
I ����"f, X<s��� 5Åq½�&³\�"f ¿º ~½Ód��×�æ ���\�¦���×þ�K���
![Page 15: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/15.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)X<s��� 5Åq½�&³�¦ ¿º ~½Ód��×�æ ����Ð ���×þ�.
I ���(interface)ܼ�Р׼�Q����H l�S\�_� �s�
empty : stk vs unit→ stkpush : stk ∗ elmt→ stk vs stk ∗ elmt→ unitis-empty? : stk→ bool vs stk→ boolpop : stk→ elmt× stk vs stk→ elmt
![Page 16: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/16.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)X<s��� 5Åq½�&³�¦ ¿º ~½Ód��×�æ ����Ð ���×þ�.
I 5Åq½�&³_� �s�
“applicative style”
(define empty ())
(define (push s x) (cons x s))
(define is-empty? null?)
(define (pop s) (if (is-empty? s) (error) s))
“imperative style”
(define empty (cons 0 0))
(define (push s x)
(let ((cell (cons x nil)))
(begin (set-cdr! cell (cdr s)) (set-cdr! s cell))
))
(define (is-empty? s) (= 0 (cdr s)))
(define (pop s)
(if (is-empty? s) (error)
(let ((top (cadr s)))
(begin (set-cdr! s (cddr s)) (cons top s))
)))
![Page 17: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/17.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)X<s��� 5Åq½�&³�¦ ¿º ~½Ód��×�æ ����Ð ���×þ�.
I 5Åq½�&³_� �s�
“applicative style”
(define empty ())
(define (push s x) (cons x s))
(define is-empty? null?)
(define (pop s) (if (is-empty? s) (error) s))
“imperative style”
(define empty (cons 0 0))
(define (push s x)
(let ((cell (cons x nil)))
(begin (set-cdr! cell (cdr s)) (set-cdr! s cell))
))
(define (is-empty? s) (= 0 (cdr s)))
(define (pop s)
(if (is-empty? s) (error)
(let ((top (cadr s)))
(begin (set-cdr! s (cddr s)) (cons top s))
)))
![Page 18: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/18.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs
imperative programming)X<s��� 5Åq½�&³�¦ ¿º ~½Ód��×�æ ����Ð ���×þ�.
I 5Åq½�&³_� �s�
“applicative style”
(define empty ())
(define (push s x) (cons x s))
(define is-empty? null?)
(define (pop s) (if (is-empty? s) (error) s))
“imperative style”
(define empty (cons 0 0))
(define (push s x)
(let ((cell (cons x nil)))
(begin (set-cdr! cell (cdr s)) (set-cdr! s cell))
))
(define (is-empty? s) (= 0 (cdr s)))
(define (pop s)
(if (is-empty? s) (error)
(let ((top (cadr s)))
(begin (set-cdr! s (cddr s)) (cons top s))
)))
![Page 19: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/19.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd�� áÔ�ÐÕªA�bç(applicative programming) "é¶
o�
♣I °úכ�Ér ��� �t� ·ú§��H �כ
I �<Êú��H °ú�כ¦ ~ÃÎ�� Dh�Ðî�r °ú�כ¦ ëß���H��
I °úכ[þt�Ér ��� �t� ·ú§Ü¼Ù¼�Ð þj@/ô�Ç /BNÄ» ��2�¤ ½�&³|cú e����
I See: l�>�\�"f ½�&³÷&��H (append l r),(add-element S 1) (\V: S = {0, 2, 3, 4, 5, 6})
![Page 20: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/20.jpg)
SNU 4190.210 c©Kwangkeun Yi
Óüt|�×�æd�� áÔ�ÐÕªA�bç(imperative programming)
"é¶o�
♣I Óüt|��Ér Õª �©�I��� ��� ���H �כ
I �<Êú��H Óüt|��¦ ~ÃÎ�� l��>r_� Óüt|��¦ ����or�~� ú e����
I Óüt|��Ér ��� �Ù¼�Ð /BNÄ» ��2�¤ ½�&³÷&��� �D¥1lxÛ¼�QÖ�¦ úe����
I See: l�>�\�"f ½�&³÷&��H (append l r),(add-element S 8)
I ��� ���H ×�æçß�\� ��6 x÷&Ù¼�Ð, í�H"f�� ×�æ¹כ
![Page 21: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/21.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd��õ� Óüt|�×�æd�� ½�&³_� q�6 x q��§
I S = {0, 2, 3, 4, 5, 6}I ½�&³: s���� �ÃÐÒ�o ��t�½�(binary search tree)
I ��6£§ �<Êú_� ½�&³: (add-element S 9)
![Page 22: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/22.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd��õ� Óüt|�×�æd�� ½�&³_� q�6 x q��§��6£§ �<Êú_� ½�&³: (add-element S 9)
I ����or�v����"f(imperative style)
I ����o\O�s�(applicative style)
![Page 23: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/23.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd��õ� Óüt|�×�æd�� ½�&³_� q�6 x q��§��6£§ �<Êú_� ½�&³: (add-element S 9)
I ����or�v����"f(imperative style)
I ����o\O�s�(applicative style)
![Page 24: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/24.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd��õ� Óüt|�×�æd�� ½�&³_� q�6 x q��§��6£§ �<Êú_� ½�&³: (add-element S 9)
I ����or�v����"f(imperative style)
I ����o\O�s�(applicative style)
![Page 25: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/25.jpg)
SNU 4190.210 c©Kwangkeun Yi
°úכ×�æd��õ� Óüt|�×�æd�� ½�&³_� q�6 x q��§
"é¶�è\�¦ V,���H �âĺ
I ����o�)a Dh��ëßכ Ä»t� ���� ÷&��H �âĺtime space
imperative O(log N) O(1)applicative O(log N) O(1)
I "º��õכ Dh�כ �¿º Ä»t�K��� ���H �âĺtime space
imperative O(N) O(N)applicative O(log N) O(log N)
![Page 26: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/26.jpg)
SNU 4190.210 c©Kwangkeun Yi
��6£§
1 °úכ×�æd�� vs Óüt|�×�æd�� áÔ�ÐÕªA�bç(applicative vs imperativeprogramming)
2 áÔ�ÐÕªÏþ�_� s�K�: 8��âõ� Bj�o�(environment &memory)
![Page 27: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/27.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��âõ� Bj�o�(environment & memory)
áÔ�ÐÕªÏþ� z�'���¦ s�K� ���H X< ¹ô�Çכ��9 ¿º>h_� z��
I 8��â: áÔ�ÐÕªÏþ�\�"f &ñ_��)a s�2£§[þtõ� Õª @/�©�_�3lq2�¤³ð
I 8��â�Ér #�!Á: áÔ�ÐÕªÏþ�_� #Qn�\�¦ z�'�� ���\� �������Ér 8��âs� ��6 xH�d
I Ä»ò#3�0A\� ���� ����o ���H s�2£§_� @/�©��¦ ����� ���H
X< ¹כ��9
I Bj�o�: s�2£§s� t�g�A ���H Õª @/�©�(°úכs��� Óüt|�)�¦½�&³ ���H /BNçß�
I Bj�o���H ���: Bj�o���H áÔ�ÐÕªÏþ� r����M:ÂÒ'�=åQ±ú�M:��t� ���
I z�'��×�æ\� ����o ���H Óüt|��¦ s�K� ���H X< .¹כ��9 ½�&³�)a/BNçß�\�"f Óüt|�[þts� ����o.
![Page 28: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/28.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��âõ� Bj�o�(environment & memory)
áÔ�ÐÕªÏþ� z�'���¦ s�K� ���H X< ¹ô�Çכ��9 ¿º>h_� z��
I 8��â: áÔ�ÐÕªÏþ�\�"f &ñ_��)a s�2£§[þtõ� Õª @/�©�_�3lq2�¤³ð
I 8��â�Ér #�!Á: áÔ�ÐÕªÏþ�_� #Qn�\�¦ z�'�� ���\� �������Ér 8��âs� ��6 xH�d
I Ä»ò#3�0A\� ���� ����o ���H s�2£§_� @/�©��¦ ����� ���H
X< ¹כ��9
I Bj�o�: s�2£§s� t�g�A ���H Õª @/�©�(°úכs��� Óüt|�)�¦½�&³ ���H /BNçß�
I Bj�o���H ���: Bj�o���H áÔ�ÐÕªÏþ� r����M:ÂÒ'�=åQ±ú�M:��t� ���
I z�'��×�æ\� ����o ���H Óüt|��¦ s�K� ���H X< .¹כ��9 ½�&³�)a/BNçß�\�"f Óüt|�[þts� ����o.
![Page 29: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/29.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��âõ� Bj�o�(environment & memory)
áÔ�ÐÕªÏþ� z�'���¦ s�K� ���H X< ¹ô�Çכ��9 ¿º>h_� z��
I 8��â: áÔ�ÐÕªÏþ�\�"f &ñ_��)a s�2£§[þtõ� Õª @/�©�_�3lq2�¤³ð
I 8��â�Ér #�!Á: áÔ�ÐÕªÏþ�_� #Qn�\�¦ z�'�� ���\� �������Ér 8��âs� ��6 xH�d
I Ä»ò#3�0A\� ���� ����o ���H s�2£§_� @/�©��¦ ����� ���H
X< ¹כ��9
I Bj�o�: s�2£§s� t�g�A ���H Õª @/�©�(°úכs��� Óüt|�)�¦½�&³ ���H /BNçß�
I Bj�o���H ���: Bj�o���H áÔ�ÐÕªÏþ� r����M:ÂÒ'�=åQ±ú�M:��t� ���
I z�'��×�æ\� ����o ���H Óüt|��¦ s�K� ���H X< .¹כ��9 ½�&³�)a/BNçß�\�"f Óüt|�[þts� ����o.
![Page 30: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/30.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��âõ� Bj�o�(environment & memory)
![Page 31: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/31.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â(environment)
I 8��â�Ér s�2£§[þts� Áº%Á�¦ t�g�A ���H t�\�¦ ·ú��9ÅÒ��H_�s��¦
I s�2£§õ� Õª @/�©�_� �©�(binding)[þt_� _�s��¦
I ���H áÔ�ÐÕªÏþ�d��_� z�'���Ér ÅÒ#Q��� 8��â��A�\�"f
���'���)a��
I 8��â�Ér áÔ�ÐÕªÏþ�d���¦ �����Ð��H îß��â
I (+ x y)_� z�'�����õ���H?I ���Ér 8��â\�"f ���Ér z�'�����õ�\�¦ ÍÇr
![Page 32: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/32.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â(environment) �'ao�
I 8��â ëß�[þtl�: s�2£§s� t�#Qt����
I 8��â �ÃÐ� �l�: s�2£§s� ���������
I 8��â �l� �l�: Ä»ò#3�0A�� =åQ�����
![Page 33: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/33.jpg)
SNU 4190.210 c©Kwangkeun Yi
Dh�Ðî�r 8��âs� �{9�÷&��H �âĺ
s�2£§f±��H �âĺ(binding, declaration, definition)
I d��\�"f s�2£§f±l�
E ::= · · · \V����þt]כ
| (let ((x E)+) E) x_� &ñ_�| (letrec ((x E)+) E) x_� F�) &ñ_�| (E E) �<Êú ñØ�¦r� �<Êú������� &ñ_�H�d
I áÔ�ÐÕªÏþ�\�"f s�2£§f±l�
P ::= E >�íß�d��
| (define x E)∗ E s�2£§&ñ_� Êê >�íß�d��
![Page 34: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/34.jpg)
SNU 4190.210 c©Kwangkeun Yi
Bj�o�_� Óüt|��¦ ����or�v���H �âĺ
����o r�v���H "î§îë�H(mutation, imperative operations)
I Scheme: set!, set-car!, set-cdr!
I OCaml: t�&ñë�H(:=)
I Java, C, C++1px: ���H t�&ñë�H(=)
![Page 35: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/35.jpg)
SNU 4190.210 c©Kwangkeun Yi
�<Êúü< 8��â
�<Êú = �<Êú %7�Û¼àÔ &ñ_�ü< �<Êú�� &ñ_�|c M:_� 8��â
(let ((y 1))
(let ((udd (lambda (x) (+ x y))))
(let ((y 10))
(udd 8))))
![Page 36: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/36.jpg)
SNU 4190.210 c©Kwangkeun Yi
�<Êúü< 8��â
�<Êú = �<Êú %7�Û¼àÔ &ñ_�ü< �<Êú�� &ñ_�|c M:_� 8��â
(let ((y 1))
(let ((udd (lambda (x) (+ x y))))
(let ((y 10))
(udd 8))))
![Page 37: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/37.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 38: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/38.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 39: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/39.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 40: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/40.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 41: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/41.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 42: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/42.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 43: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/43.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define x 1)
(set! x (+ x 1))
(* (let ((x (+ x 2)))
(+ x 3))
x)
(define f (lambda (n) (+ n x)))
(f 10)
(let ((x 100))
(f 10))
![Page 44: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/44.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define (make-counter n)
(lambda ()
(begin (set! n (+ n 1)) n)
))
(define tic1 (make-counter 0))
(tic1)
(tic1)
(define tic2 (make-counter 0))
(tic2)
(tic1)
![Page 45: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/45.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define (make-counter n)
(lambda ()
(begin (set! n (+ n 1)) n)
))
(define tic1 (make-counter 0))
(tic1)
(tic1)
(define tic2 (make-counter 0))
(tic2)
(tic1)
![Page 46: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/46.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define (make-counter n)
(lambda ()
(begin (set! n (+ n 1)) n)
))
(define tic1 (make-counter 0))
(tic1)
(tic1)
(define tic2 (make-counter 0))
(tic2)
(tic1)
![Page 47: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/47.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define (make-counter n)
(lambda ()
(begin (set! n (+ n 1)) n)
))
(define tic1 (make-counter 0))
(tic1)
(tic1)
(define tic2 (make-counter 0))
(tic2)
(tic1)
![Page 48: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/48.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define (make-counter n)
(lambda ()
(begin (set! n (+ n 1)) n)
))
(define tic1 (make-counter 0))
(tic1)
(tic1)
(define tic2 (make-counter 0))
(tic2)
(tic1)
![Page 49: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/49.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq s�K� �l�
(define (make-withraw balance)
(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"insufficient fund"
)))
(define withdraw (make-withdraw 100))
(withdraw 10)
(withdraw 20)
![Page 50: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/50.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq�Ð s�K� �l�
(define empty (cons () ()))
(define (push s x)
(let ((cell (cons x ())))
(begin (set-cdr! cell (cdr s))
(set-cdr! s cell))
(define stk empty)
(push stk 1)
(push stk 2)
![Page 51: SNU 4190.210 ÆÔŒ—Õ“A b ض (Principles of Programming) Part III · SNU 4190.210 c Kwangkeun Yi SNU 4190.210 ÆÔŒ—Õ“A b ض˙(Principles of Programming) Part III Prof](https://reader034.vdocuments.us/reader034/viewer/2022042419/5f361c46e6ca280e2d4501af/html5/thumbnails/51.jpg)
SNU 4190.210 c©Kwangkeun Yi
8��â�4Sq�Ð s�K� �l�
(define empty (cons () ()))
(define (push s x)
(let ((cell (cons x ())))
(begin (set-cdr! cell (cdr s))
(set-cdr! s cell))
(define stk empty)
(push stk 1)
(push stk 2)