Exercise 1.11

(define (exam n)

  (cond ((< n 3) n)

        (else (+ (exam (- n 1)) (* 2 (exam (- n 2))) (* 3 (exam (- n 3)))))))

(exam 10)



(define (exam-iter a b c count)

  (if (< count 3)

      a

        (exam-iter (+ a (* 2 b) (* 3 c)) a b (- count 1))))


(define (exam2 n)

  (exam-iter 2 1 0 n))

(exam2 10)


Exercise 1.12

(define (f a b)

  (cond ((= b 1) 1)

        ((= a b) 1)

        (else (+ (f (- a 1) (- b 1)) (f (- a 1) b)))))

(f 2 2)

(f 4 3)

(f 4 1)

(f 5 3)


Exercise 1.13

f(n)이 phi^n/sqrt(5)에 근사함을 증명하라

힌트 : phi = (1+sqrt(5))/2, psi = (1-sqrt(5))/2, 귀납법을 사용하라, 피보나치 정의를 이용해 f(n)= (phi^n-psi^n)/sqrt(5) 를 증명

피보나치 수열 정의에 따라 f(n+2) = f(n+1) + f(n) 이므로

(phi^(n+1)-psi^(n+1))/sqrt(5) + (phi^n-psi^n)/sqrt(5)

=[(phi^(n+1)-psi^(n+1))+(phi^n-psi^n)]/sqrt(5)

=[(phi*phi^n-psi*psi^n)+(phi^n-psi^n)]/sqrt(5)

=[(phi+1)*phi^n-(psi+1)*psi^n)]/sqrt(5) 이다.

또한 (phi^(n+2)-psi^(n+2))/sqrt(5) 

= (phi^2*phi^n-psi^2*psi^n)/sqrt(5) 이고, 이것이 위 식과 같아야 한다.

따라서 phi+1 = phi^2 와 psi+1=psi^2가 성립해야 한다.

그런데 직접 계산해보면 성립 한다.

따라서 f(n)= (phi^n-psi^n)/sqrt(5) 가 합당하다.

여기서  |psi| < 1 이므로 psi^n에서 n이 무한대라면 0으로 수렴한다.

따라서 f(n)이 phi^n/sqrt(5)에 근사한다.


Exercise 1.16

(define (even? n)

  (= (remainder n 2) 0))

(define (exp_iter b n a)

  (cond ((= n 0) a)

        ((even? n) (exp_iter (square b) (/ n 2) a))

        (else (exp_iter b (- n 1) (* a b)))))


Exercise 1.17

(define (even? n)

  (= (remainder n 2) 0))

(define (exp_iter b n a)

  (cond ((= n 0) a)

        ((even? n) (exp_iter (square b) (/ n 2) a))

        (else (exp_iter b (- n 1) (* a b)))))



Exercise 1.18

  (define (fast2* a b n) 

   (cond ((= n 0) a) 

         ((even? n) (fast2* a (double b) (halve n)))

         (else (fast2* (+ a b) b (- n 1)))))


Exercise 1.19

(+ (square p) (square q)) 

                    (+ (* 2 p q) (square q)) 


Exercise 1.20

normal:18번

applicative:4번

'코딩 > SICP' 카테고리의 다른 글

SICP Exercise 1.1 ~ 1.10  (0) 2016.07.19
Posted by 타다키치
,

MyBatis를 쓰는데 리턴 타입을 Java 8의 LocalDate 같은 Java.time 하위 클래스로 바로 받을 수가 없다.


이를 위해서는 TypeHandler를 추가해야 한다.



			org.mybatis
			mybatis-spring
			
			1.3.0
		
		
			org.mybatis
			mybatis
			3.4.0
			
		
		
		    org.mybatis
		    mybatis-typehandlers-jsr310
		    1.0.0
		

위와 같이 mybatis-spring과 mybatis의 버전을 올려줬다. 그리고 mybatis-typehandlers-jsr310를 추가로 등록해줬다.


그럼 아래와 같이 리턴 타입을 Java 8의 java.time 하위 클래스로 받을 수 있다.

@Select("select date from tbl_index "
			+ "where date <= #{param} order by date desc limit 1 ")
	public LocalDate getDate(LocalDate param);



Posted by 타다키치
,

Exercise 1.1 

10

(+ 5 3 4)

(- 9 1)

(/ 6 2)

(+ (* 2 4) (- 4 6))

(define a 3)

(define b (+ a 1))

(+ a b (* a b))

(= a b)

(if (and (> b a) (< b (* a b)))

    b

    a)

(cond ((= a 4) 6)

      ((= b 4) (+ 6 7 a))

      (else 25))

(+ 2 (if(> b a) b a))

(* (cond ((> a b) a)

         ((< a b) b)

         (else -1))

   (+ a 1))




Exercise 1.2

(/ (+ 5 4 (- 2 (- 3 (+ 6 4/5))))(* 3 (- 6 2)(- 2 7)))


Exercise 1.3

(define (square x)

  (* x x))


(define (sum-of-squares x y)

  (+ (square x) (square y)))


(define (sss x y z)

  (cond ((and (> x z) (> y z)) (sum-of-squares x y))

        ((and (> y x) (> z x)) (sum-of-squares y z))

        ((and (> x y) (> z y)) (sum-of-squares x z))

        ))


(sss 4 3 4)


Exercise 1.5

applicative-order에서는 (test 0 (p))가 절대 안끝난다. 왜냐면 (p)가 무한대로 불러지기 때문이다.

normal-order evaluation에서는 표현의 평가가 차례로 이뤄지므로 결과가 나온다.


Exercise 1.6

무한루프에 빠짐. Scheme은 applicative 방식이라 operand를 먼저 평가하기 때문임.


Exercise 1.7

(define (new-good-enough? guess x)

  (< (abs (- guess (improve guess x))) 0.0000001))


Exercise 1.8

(define (cube-improve guess x)

   (/ (+ (/ x (square guess)) (* 2 guess))

     3))


(cube-improve 3 27)


(define (cube-good-enough? guess x)

  (< (abs (- guess (cube-improve guess x))) 0.0000001))


(define (cube-iter guess x)

  (if (cube-good-enough? guess x)

      guess

      (cube-iter (cube-improve guess x)

                 x)))


(define (cube x)

  (cube-iter 1.0 x))


(cube 64.0)



Exercise 1.9

(+ 4 5)                        

(inc (+ (dec 4) 5))            

(inc (+ 3 5))                  

(inc (inc (+ 2 5)))            

(inc (inc (inc (+ 1 5))))      

(inc (inc (inc (inc (+ 0 5)))))

(inc (inc (inc (inc 5))))      

(inc (inc (inc 6)))            

(inc (inc 7))                  

(inc 8)                        

9                              

                               

(+ 4 5)                        

(+ (dec 4) (inc 5))            

(+ 3 6)                        

(+ (dec 3) (inc 6))            

(+ (dec 2) (inc 7))            

(+ (dec 1) (inc 8))            

(+ 0 9)                        

(9)                            


Exercise 1.10

(f n)2n
(g n)2^{n}
(h n)2^2^… (n-1 times)

Posted by 타다키치
,