Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

scheme question

Name: Anonymous 2013-03-31 11:27

Hello /prog/, I'm not really much of a programmer but I've been dicking around with Lisp/Scheme lately for fun (DrRacket specifically).  One of the programs in my book goes like this:

;; move-circle : number circle  ->  circle
;; to draw and clear a circle, translate it by delta pixels
(define-struct circle (center radius color))

(define (move-circle delta a-circle)
  (cond
    [(draw-and-clear-circle a-circle) (translate-circle a-circle delta)]
    [else a-circle]))

draw-and-clear-circle draws and clears a circle, obviously, and translate-circle simply modifies the given "circle" structure such that its position is shifted by "delta" pixels.  So if I wanted to make a circle move across the screen I could run something like

(draw-a-circle (move-circle 10 (move-circle 10 (move-circle 10 (make-circle (make-posn 30 30) 45 'red)))))

Anyway my question is simply this: why is there an "else" at all in that conditional?  The function "draw-and-clear-circle" always returns a value of "true", or, if there's something wrong with the definition of the circle structure, an error.  So afaik the "else" would never come into play.

Name: Anonymous 2013-03-31 18:53

>>11
Not really. Try this in clos.


(define (integer-box value)
  (lambda (mesg . args)
    (case mesg
      ((value) value)
      ((+) (integer-box (apply + (cons value (map (lambda (o) (o 'value)) args)))))
      ((-) (integer-box (apply - (cons value (map (lambda (o) (o 'value)) args)))))
      ((*) (integer-box (apply * (cons value (map (lambda (o) (o 'value)) args)))))
      ((/) (integer-box (apply / (cons value (map (lambda (o) (o 'value)) args)))))
      (else (error mesg)))))

(define (list-box lis)
  (lambda (mesg . args)
    (case mesg
      ((value) lis)
      ((concat) (list-box (append (cons lis (map (lambda (o) (o 'value)) args)))))
      (else (list-box (map (lambda (o) (apply o (cons mesg args))) lis))))))


(define e (list-box (map integer-box '(1 2 3 4 5 6 7))))

(define f (e '+ (integer-box 10)))

(display (map (lambda (o) (o 'value)) (e 'value))) (newline)
(display (map (lambda (o) (o 'value)) (f 'value))) (newline)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List