Home

Archivos

Buscar

Categorías

Feeds:

RSS / Atom

Programacion HDTP Capitulo 6 - 1· 2. January 2008, 23:01

Tengo que volver a hacer algo de Ruby y C# porque lo he abandonado las ultimas semanas, mientras tanto he seguido con Scheme

Aqui van los primeros ejercicios para el capitulo 6

(define (distance-to-0 a-posn)
  (sqrt
    (+ (sqr (posn-x a-posn))
       (sqr (posn-y a-posn)))))
;6.1.1
;(distance-to-0 (make-posn 3 4))
;(distance-to-0 (make-posn (* 2 3) (* 2 4)))
;(distance-to-0 (make-posn 12 (- 6 1)))
;6.2.1
;(start 300 300)
;(draw-solid-line (make-posn 10 10) (make-posn 110 10) 'red)
;(draw-solid-rect (make-posn 10 30) 100 50 'blue)
;(draw-circle (make-posn 110 30) 30 'yellow)
;(draw-solid-disk (make-posn 10 80) 50 'green)
;(stop)
;6.2.2
;; dimensions of traffic light    [curriculum1aa-Z-G-18.gif]
(define WIDTH 50)
(define HEIGHT 160)
(define BULB-RADIUS 20)
(define BULB-DISTANCE 10)
;; the positions of the bulbs 
(define X-BULBS (quotient WIDTH 2))
(define Y-RED (+ BULB-DISTANCE BULB-RADIUS))
(define Y-YELLOW (+ Y-RED BULB-DISTANCE (* 2 BULB-RADIUS)))
(define Y-GREEN (+ Y-YELLOW BULB-DISTANCE (* 2 BULB-RADIUS)))
;; draw the light with the red bulb turned on
(start WIDTH HEIGHT)
(draw-solid-disk (make-posn X-BULBS Y-RED) BULB-RADIUS 'red)
(draw-circle (make-posn X-BULBS Y-YELLOW) BULB-RADIUS 'yellow)
(draw-circle (make-posn X-BULBS Y-GREEN) BULB-RADIUS 'green)
;6.2.2
;; clear-bulb : symbol -> boolean
;;el and es necesario porque el lenguaje begginer solo acepta una expresion por funcion. Y como las expresiones de dibujo regresan true, cada una es expresion. De hecho viene explicado en el mismo parrafo, pero no lo lei bien la primera vez
(define (clear-bulb color)
  (and (clear-solid-disk (make-posn X-BULBS (get-y color)) BULB-RADIUS color)(draw-circle (make-posn X-BULBS (get-y color)) BULB-RADIUS color)))
;helper-function get-y
(define (get-y color)
  (cond 
    [(symbol=? color 'red) Y-RED]
    [(symbol=? color 'yellow) Y-YELLOW]
    [(symbol=? color 'green) Y-GREEN]
    [else 'ERROR]))
;6.2.3
; draw-bulb symbol -> true
(define (draw-bulb color)
  (draw-solid-disk (make-posn X-BULBS ( get-y color)) BULB-RADIUS color))
;6.2.4
;switch : symbol,symbol -> true
(define (switch color-off color-on)
  (and (clear-bulb color-off) (draw-bulb color-on)))