読者です 読者をやめる 読者になる 読者になる

SICPゼミ第9回

練習問題2.43

(queen-cols (- k 1)) をk回呼び出しているのでごみ。

注意

これから先の範囲は図形言語を前提としているので、tube は Racket で、ほか三人は Gauche でがんばって図形言語を入れました。

Racket での図形言語

一行目に

(require (planet soegaard/sicp:2:1/sicp))

を入れておしまい。

(paint einstein)

アインシュタイン先生の画像が出力されればOK. einstein が変数で画像データ(?) っぽくなっているので beside や below で編集したあと最後に(paint ~~) で描画。

by tube

Gauche での図形言語

がんばって~~~~~~~~(^^)/~~~~

練習問題2.44
(require (planet soegaard/sicp:2:1/sicp))
(define (up-split painter n)
  (if (= n 0)
      painter
      (let ((smaller (up-split painter (- n 1))))
        (below painter (beside smaller smaller)))))
(define (corner-split painter n)
  (if (= n 0)
      painter
      (let ((up (up-split painter (- n 1)))
            (right (right-split painter (- n 1))))
        (let (( top-left (beside up up))
              (bottom-right (below right right ))
              (corner (corner-split painter (- n 1))))
          (beside (below painter top-left)
                  (below bottom-right corner ))))))

実行結果

(paint (corner-split einstein 4))

f:id:sicp-zemi:20160419194410p:plain

by tube

練習問題2.45
(define (split proc1 proc2)
  (define (split-sub painter n)
    (if (= n 0)
      painter
      (let (( smaller (right-split painter (- n 1))))
        (proc1 painter (proc2 smaller smaller )))))
  split-sub)
(define right-split (split beside below ))
(define up-split (split below beside ))

by tube

練習問題2.46

ベクトル演算って前やんなかったっけ。

(define (make-vect x y)
  (cons x y))
(define (xcor-vect v)
  (car v))
(define (ycor-vect v)
  (cdr v))
(define (add-vect v1 v2)
  (make-vect (+ (xcor-vect v1) (xcor-vect v2))
             (+ (ycor-vect v1) (ycor-vect v2))))
(define (sub-vect v1 v2)
  (make-vect (- (xcor-vect v1) (xcor-vect v2))
             (- (ycor-vect v1) (ycor-vect v2))))
(define (scale-vect s v)
  (make-vect (* s (xcor-vect v)) (* s (ycor-vect v))))
練習問題2.47
(define (make-frame origin edge1 edge2)
  (list origin edge1 edge2))

(define (origin-frame frame)
  (car frame))
(define (edge1-frame frame)
  (cadr frame))
(define (edge2-frame frame)
  (caddr frame))

consのほうはcaddrをcddrにするだけ。

by tube

練習問題2.48
(define (make-segment v1 v2)
  (cons v1 v2))
(define (start-segment seg)
  (car seg))
(define (end-segment seg)
  (cdr seg))
練習問題2.49

draw-line がないのでライブラリにある segments->painter を使います。
脳筋列挙プログラム万歳。

(define (frame-painter frame)
  (let ((o (origin-frame frame))
        (a (add-vect (origin-frame frame) (edge1-frame frame)))
        (b (add-vect (origin-frame frame) (edge2-frame frame)))
        (c (add-vect (add-vect (origin-frame frame) (edge1-frame frame))
                     (edge2-frame frame))))
    (paint
     (segments->painter
      (list (make-segment o a)
            (make-segment o b)
            (make-segment a c)
            (make-segment c b))))))

(define (diag frame)
  (let ((o (origin-frame frame))
        (a (add-vect (origin-frame frame) (edge1-frame frame)))
        (b (add-vect (origin-frame frame) (edge2-frame frame)))
        (c (add-vect (add-vect (origin-frame frame) (edge1-frame frame))
                     (edge2-frame frame))))
    (paint
     (segments->painter
      (list (make-segment a b)
            (make-segment o c))))))
(define (central frame)
   (let* ((o (origin-frame frame))
          (a (add-vect (origin-frame frame) (edge1-frame frame)))
          (b (add-vect (origin-frame frame) (edge2-frame frame)))
          (c (add-vect (add-vect (origin-frame frame) (edge1-frame frame))
                       (edge2-frame frame)))
          (oa (scale-vect 0.5 (add-vect o a)))
          (ob (scale-vect 0.5 (add-vect o b)))
          (ac (scale-vect 0.5 (add-vect a c)))
          (bc (scale-vect 0.5 (add-vect b c))))
     (paint
      (segments->painter
       (list (make-segment oa ob)
             (make-segment ob bc)
             (make-segment bc ac)
             (make-segment oa ac))))))
         

wave は何したらよいのかわからないのでパス。
実行結果
(define o (make-vect 0.1 0.1))
(define v1 (make-vect 0.2 0.4))
(define v2 (make-vect 0.3 0.2))
(define f (make-frame o v1 v2))

(frame-painter f)
f:id:sicp-zemi:20160419211857p:plain
(diag f)
f:id:sicp-zemi:20160419211908p:plain
(central f)
f:id:sicp-zemi:20160419211916p:plain

by tube