;;;      Definitions to accompany LLL: Problem Set #1
;;;      --------------------------------------------

;;; 
;;; 

(define SPIRAL
   (lambda [start end unit direction]
      (for i start end
         (begin (forward (* i unit))
                (direction 90)))))

(define WHORL
   (lambda [n unit direction]
      (begin (spiral n 1 unit direction)
             (forward unit)
             ((other-direction direction) 90)
             (spiral 1 n unit (other-direction direction)))))

(define BORDER
   (lambda [m n unit direction]
      (for i 1 m
         (begin (whorl n unit direction)
                (forward (* n unit))
                (direction 90)))))

(define OTHER-DIRECTION
   (lambda [direction]
      (cond [(= ↑direction ↑right) left]	; Ignore the ↑s for now.
            [(= ↑direction ↑left) right]	; Ignore the ↑s for now.
            [$true (error "Unrecognized direction")])))

(define CORNER
   (lambda [n unit direction]
      (begin (forward (* (1+ n) unit))
             (direction 90))))

(define RECTANGLE
   (lambda [width height n unit direction]
      (begin (border (1- width) n unit direction)
             (corner n unit direction)
             (border (1- height) n unit direction)
             (corner n unit direction)
             (border (1- width) n unit direction)
             (corner n unit direction)
             (border (1- height) n unit direction)
             (corner n unit direction))))

(define THICK-FORWARD
   (lambda [thickness]
      (lambda [size]
         (begin (for i 1 thickness
                   (if (> i 1)
                       (begin (right 90) (forward (1- i)) (right 90))
                       "ok")
                   (forward size))
                (if (= thickness 1)
                    "ok"
                    (begin (right 90)
                           (jump (/ thickness 2))
                           (if (even thickness)
                               (begin (right 90) (jump size))
                               (left 90))))))))

(define TURTLE-WRITE
   (lambda [string thickness]
      (string-map (lambda [character]
                     (((assoc character *drawing-table*)
                       (thick-forward thickness))))
                  string)
      "ok"))