# weima learns to program

my attempt to do the exercises in sicp.

## Sunday, July 20, 2008

### sicp exercise 2.49

;; Exercise 2.49.  Use segments->painter to define the following primitive painters:

;; a.  The painter that draws the outline of the designated frame.

;; b.  The painter that draws an "X" by connecting opposite corners of the frame.

;; c.  The painter that draws a diamond shape by connecting the midpoints of the sides of the frame.

;; d.  The wave painter.

(define (make-vect x y) (cons x y))

(define (make-segment start end) (cons start end))
(define (start-segment line-segment) (car line-segment))
(define (end-segment   line-segment) (cdr line-segment))

(define (segments->painter segment-list)
(lambda (frame)
(for-each
(lambda (segment)
(draw-line
((frame-coord-map frame) (start-segment segment))
((frame-coord-map frame) (end-segment segment))))
segment-list)))

;; a.

(define outline-segments
(list (make-segment (make-vect 0 0) (make-vect 0 1))
(make-segment (make-vect 0 1) (make-vect 1 1))
(make-segment (make-vect 1 1) (make-vect 1 0))
(make-segment (make-vect 1 0) (make-vect 0 0))))

(define (outline-painter frame) (segments->painter outline-segments))

;; b.

(define X-segments
(list (make-segment (make-vect 0 0) (make-vect 1 1))
(make-segment (make-vect 1 0) (make-vect 0 1))))

(define (X-painter frame) (segments->painter X-segments))

;; c.

(define diamond-segments
(list (make-segment (make-vect 0.0 0.5) (make-vect 0.5 1.0))
(make-segment (make-vect 0.5 1.0) (make-vect 1.0 0.5))
(make-segment (make-vect 1.0 0.5) (make-vect 0.5 0.0))
(make-segment (make-vect 0.5 0.0) (make-vect 0.0 0.5))))

(define (diamond-painter frame) (segments->painter diamond-segments))

;; d.

;; too much work :(