sicp ch3 1 - andstudy/forge GitHub Wiki
- ์๊ธ์์ ๋์ ์ธ์ถํ๋ ์์
-
๋ฒ์ 1: ์ธ๋ถ๋ณ์ ์ฌ์ฉ
-
(set! ) : ์ ๋ด์ฉ์ ๋ก ๋ฐ๊พผ๋ค
-
(begin ... ) : ๋ค์ ์ฐจ๋ก๋ก ์คํํ๊ณ ๋ง์ง๋ง์ ๋ฐํ
( define balance 100) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (withdraw 10) (withdraw 20)
-
-
๋ฒ์ 2: ํจ์ ์์ผ๋ก ๋ณ์ ์๋
-
๋ง๋ฐ๊ฟ ๊ณ์ฐ๋ฒ์ผ๋ก ์ค๋ช ์ด ๋ถ๊ฐ๋ฅํ๊ณ ํจ์๋ง ๋ณด์์๋ ์ ์ด๋ ๊ฒ ๋์ ๋๋์ง ์ค๋ช ์ด ๋์ง ์์
(define new-withdraw (let ((balance 100)) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))))
( new-withdraw 10 ) ( new-withdraw 20 ) ( new-withdraw (new-withdraw 1))
-
-
๋ฒ์ 3 : ๋ ์ฐพ๋ ํ๋ก์์ ๋ฅผ ๋ง๋ค์ด ๋ธ๋ค
-
๋ ผ๋ฆฌ์ ์ผ๋ก ์๋ฏธ๊ฐ ๋ค์ด๋จ
(define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")))
(define W1 (make-withdraw 100)) (define W2 (make-withdraw 200))
(W1 10) (W2 20)
-
-
์๊ธ์ ๋ฃ๊ณ ๋นผ๋ ์ญํ ์ ์ํํ๋ ๊ฐ์ฒด (๋ฉ์ธ์ง ์ด์ฉ)
(define (make-account balance) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) (define (deposit amount) (set! balance (+ balance amount)) balance) (define (dispatch m) (cond ((eq? m 'withdraw) withdraw) ((eq? m 'deposit) deposit) (else (error "Unknown request -- MAKE-ACCOUNT"m)))) dispatch) (define acc (make-account 100)) ((acc 'withdraw) 50) ((acc 'deposit) 40)
-
- p.296~297 ์์ค๋ฅผ ๋ณด์
- rand ํจ์๊ตฌํ์ ๋ค์ ์ํ๋ฅผ ๋ณด๊ดํ๊ณ ์์ ์ ์๊ฒ ๋์ด ๊ตฌํ์ด ๊ฐ๊ฒฐ
- ์ํ๋ณ์์ด์ฉํ rand๋ฅผ ๊ตฌํ์ ์ด์ฉํ์ฌ ๋ชฌํ
์นด๋ฅผ๋ก ์ ์์ ๋ฐ๋ฅธ ์๋ฌผ๋ ์ด์
๊ตฌํ์ด ์ ๋ค์ด๋๊ฒ ๋จ
- ๋ฌผ์ฒด์์ ์ํ๋ฅผ ๊ฐ์ถฐ ๋ฃ์ ์ ์๊ฒ ๋์๋ค~ -> ๊ณผ์ฐ ๋ ์ข๊ฒ ๋์๋๊ฐ?
-
1,2์ฅ ์ฒ๋ผ ๋ฎ์ด์ฐ๊ธฐ ์์ด ํ๋ก๊ทธ๋จ ํ๋ ๋ฒ์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ด๋ผ๊ณ ํจ
-
์ํ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ง๋ฐ๊ฟ ๊ณ์ฐ๋ฒ์ ์ฌ์ฉํ ์ ์์
(define (make-simplified-withdraw balance) (lambda (amount) (set! balance (- balance amount)) balance)) (define W (make-simplified-withdraw 25)) (W 20) 5 (W 10) - 5 -
(W 20) ์ ์ด๋ป๊ฒ ๋์ํ๋๊ฐ? (๋ง๋ฐ๊ฟ ๊ณ์ฐ๋ฒ์ผ๋ก ํ์ธํด๋ณด๊ธฐ)
- ((make-simplified-withdraw 25) 20)
- ((lambda (amount) (set! balance (- 25 amount)) 25) 20)
- (set! balance (- 25 20)) 25
- balance์ 5๋ฅผ ๋ฎ์ด ์ผ์ผ๋ ๊ฒฐ๊ตญ 25๊ฐ ๋์๋ค? (๋ญฅ๋)
-
๋ค์ ๋์์์ !set ์ด ํด์ฃผ๋ ๊ฒ์ ๋ฌด์์ธ๊ฐ?
-
W1 ๊ณผ W2๋ ๊ฐ์์ผ์ ํ์ง๋ง , ์๋ก ๋ค๋ฅธ ๊ฒฐ๊ณผ ์์ญ์ ๊ฐ๊ณ ์๊ธฐ ๋๋ฌธ์์๋ก ๋ง๋ฐ๋์๋ ์๋ค.
(define W1 (make-simplified-withdraw 25)) (define W2 (make-simplified-withdraw 25)) (W1 20) 5 (W1 20) - 15 (W2 20) 5
-
-
๋์ ์ฐจ์ด?
-
peter์ paul์ ๊ฐ๊ฐ ์ํ ๊ฐ์ ์ ๊ฐ์ง๊ณ ์๋ค.
(define peter-acc (make-account 100)) (define paul-acc (make-account 100))
-
-
peter์ paul์ ๊ฐ์ ์ํ ๊ณ์ ์ ์ฌ์ฉํ๊ณ ์๋ค
(define peter-acc (make-account 100)) (define paul-acc peter-acc)
-
ํจ์ํ
(define (factorial n) (define (iter product counter) (if (> counter n) product (iter (* counter product) (+ counter 1)))) (iter 1 1)) ( factorial 10) -
๋ช ๋ นํ
(define (factorial n) (let ((product 1) (counter 1)) (define (iter) (if (> counter n) product (begin (set! product (* counter product)) (set! counter (+ counter 1)) (iter)))) (iter))) -
ํน์ฌ๋ set! ์ด ์๋ ๋ถ๋ถ์ ์์๊ฐ ๋ฌ๋ผ์ง๋ฉด ์๋ชป๋ ๊ฐ์ ๋ฐํํจ
-
๋์์ ์ฌ๋ฌ ํ๋ก์ธ์ค๊ฐ ๋์๊ฐ๋ ์ํฉ์ด๋ผ๋ฉด ์๋์น ์ํ ๋ฌธ์ ๊ฐ ๋ฐ์๋ ์ ์์