Skip to content

Instantly share code, notes, and snippets.

@atsushisakai-gh
Created June 14, 2015 01:05
Show Gist options
  • Save atsushisakai-gh/b3cc2fe6535de1dec451 to your computer and use it in GitHub Desktop.
Save atsushisakai-gh/b3cc2fe6535de1dec451 to your computer and use it in GitHub Desktop.
SCIP
; 1.2 ==============================================================
; (/ (+ 5 4 (- 2 (- 3 ( + 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))
; 1.3 ==============================================================
; (define (square i) (* i i))
;
; (define (func x y z)
; (if (= x (large x y))
; (+ (square x) (square (large y z)))
; (+ (square y) (square (large x z)))
; )
; )
;
; (define (large a b)
; (if (> a b) a b)
; )
; 1.4 ==============================================================
; われわれの評価モデルは, 演算子が合成式である組合せでも使えることを観察せよ. それに従って, 次の手続きの振舞いを述べよ.
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
; 演算子も動的に変化できる
; 1.5 ==============================================================
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
; ### 作用的順序
; (test 0 (p))
; (test 0 (p))
; ....... pを評価し続ける
;
; ### 正規順序
; (test 0 (p))
; 0 が帰って終わる
; 1.6 ==============================================================
; (define (sqrt-iter guess x)
; (if (good-enough? guess x)
; guess
; (sqrt-iter (improve guess x)
; x)))
;
; (define (improve guess x)
; (average guess (/ x guess)))
;
; (define (average x y)
; (/ (+ x y) 2))
;
; (define (good-enough? guess x)
; (< (abs (- (square guess) x)) 0.001))
;
; (define (sqrt x)
; (sqrt-iter 1.0 x))
;
; 1.7 ==============================================================
; (define (cbrt-iter guess x)
; (if (good-enough? guess x)
; guess
; (cbrt-iter (improve guess x)
; x)))
;
; (define (improve guess x)
; (/
; (+ (/ guess (* x x)
; (* 2.0 x))
; 3.0))
; )
;
; (define (good-enough? guess x)
; (< (abs (- (square guess) x)) 0.001))
;
; (define (cbrt x)
; (cbrt-iter 1.0 x))
; (define (new-if predicate then-clause else-clause)
; (cond (predicate then-clause)
; (else else-clause)))
; new-ifは作用的順序で処理されるので引数を評価していく。
; そのためsqrt-iterが無限ループ
; 1.9 ==============================================================
; (define (inc x) (+ x 1))
;
; (define (dec x) (- x 1))
;
; ; (define (+ a b)
; ; (if (= a 0)
; ; b
; ; (inc (+ (dec a) b))))
; ;
; ; 置き換えモデル
; ; (+ 2 3)
; ; (inc (+ 1 3))
; ; (inc (inc (+ 0 3)))
; ; (inc (inc 3))
; ; (inc 4)
; ; 5
; ; aが大きくなるほどに計算プロセスが多くなる再帰的プロセス
;
; ; (define (+ a b)
; ; (if (= a 0)
; ; b
; ; (+ (dec a) (inc b))))
; ;
; ; 置き換えモデル
; ; (+ 2 3)
; ; (+ 1 4)
; ; (+ 0 5)
; ; 5
; ; 反復的プロセス
;
;
; ; 1-10
; (define (A x y)
; (cond ((= y 0) 0)
; ((= x 0) (* 2 y))
; ((= y 1) 2)
; (else (A (- x 1)
; (A x (- y 1))))))
;
; ; (A 1 10) = 1024
; ; (A 2 4) = 65536
; ; (A 3 3) = 65546
;
; ; (define (f n) (A 0 n)) ; 2n
;
; (define (g n) (A 1 n)) ; 2^n
; ; (g 3)
; ; (A 1 3)
; ; (A 0 (A 1 2))
; ; (A 0 (A (0 (A 1 1))))
; ; (A 0 (A (0 2)))
; ; (A 0 4)
; ; 8
;
; (define (h n) (A 2 n))
; ; (h n)
; ; (A 2 n)
; ; (A (- 2 1) (A 2 (- n 1)))
; ; (A 1 (A 2 (- n 1)))
; ; (A 1 (A 1 (A 2 (- n 2))))
; ; (A 1 (A 1 (A 1 (A 2 (- n 3))))
; ; (A 1 (A 1 (A 1 (A 1 (A 2 (- n 4))))))
; ; (A 1 (A 1 (A 1 (A 1 (A 1 (A 2 (- y 5)))))))
; ; (A 1 (A 1 (A 1 (A 1 (A 1 ... (A 2 0))))))
; ; (A 1 (A 1 (A 1 (A 1 (A 1 ... 0))))) =====> 2のn乗のn乗のn乗の。。。。
;
; (define (k n) (* 5 n n))
;
; ; (define (fib n)
; ; (cond ((= n 0) 0)
; ; ((= n 1) 1)
; ; (else (+ (fib (- n 1))
; ; (fib (- n 2))))))
;
; (define (fib n)
; (fib-iter 1 0 n))
;
; (define (fib-iter a b count)
; (if (= count 0)
; b
; (fib-iter (+ a b) a (- count 1))))
;
; 1.11 ==============================================================
; ↓再帰的プロセス
; (define (f n)
; (
; if(< n 3)
; n
; (+ (f (- n 1)) (* 2 (f (- n 2))) (* 3 (f (- n 3))))
; )
; )
; ↓反復的プロセス
; n + (n + 1) + (n + 2) +...
; (define (test-sum n) (iter 0 1 n))
;
; (define (iter product counter max-count)
; (if (> counter max-count)
; product
; (iter (+ product counter) (+ 1 counter) max-count)
; )
; )
(define (f n)
(f-iter 2 1 0 n))
(define (f-iter x y z count)
(cond ((= count 0) x)
((= count 1) y)
(else (f-iter (+ x (* 2 y) (* 3 z)) x y (- count 1)))
)
)
; 1.12 ==============================================================
; 上からx番目、左からy番目の値を求める
; (上からx-1番目、左から y-1番目) + (上からx-1番目 + 左からy番目)
(define (triangle x y)
(if (or (= x y) (= y 1))
1
(+ (triangle (- x 1) (- y 1)) (triangle (- x 1) y))
)
)
; 1.13 ==============================================================
; (> <)
; 1.14 ==============================================================
; gosh> (count-change 11)
; CALL cc 11 5
; CALL cc 11 4
; CALL cc 11 3
; CALL cc 11 2
; CALL cc 11 1
; RETN cc 1
; CALL cc 6 2
; RETN cc 2
; RETN cc 3
; CALL cc 1 3
; CALL cc 1 2
; RETN cc 1
; CALL cc -9 3
; RETN cc 0
; RETN cc 1
; RETN cc 4
; CALL cc -14 4
; RETN cc 0
; RETN cc 4
; CALL cc -39 5
; RETN cc 0
; RETN cc 4
; 4
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment