Josh
Josh

Reputation: 519

data definitions DrRacket?

I am having problem with this. It's quite long.

First, heres the data definition for this

(define-struct ball (x y color))    
;; Ball = (make-ball Number Number Color)    
;; Color is one of 'red, 'yellow, 'blue, etc. 

Heres my program

(require 2htdp/image)
(require 2htdp/universe)



;;An LoB is one of
;;--empty
;;--(cons ball LoB)

;;(define (ball-template lob)
;;  (cond
;;    [(empty? lob) ...]
;;   [else
;;     (first lob)...
;;    (ball-template (rest lob))]))

;;lob-length : LoB -> number
;;Counts the balls in the list
(define (lob-length lob)
  (cond
    [(empty? lob) 0]
    [else
     (add1 (lob-length (rest lob)))]))
;;Examples
(check-expect (lob-length empty) 0)
(check-expect (lob-length(cons (make-ball 1 2 "red")
                               (cons(make-ball 3 3 "blue")
                                    (cons(make-ball 5 86 "white")empty))))3)



;;lob-draw : LoB -> Scene
;;Adds the balls to an empty scene in the form of circles
(define (lob-draw lob)
  (cond
    [(empty? lob) (empty-scene 300 300)]
    [else
     (place-image (circle 3 "solid" (ball-color (first lob)))
                  (ball-x (first lob))
                  (ball-y (first lob))
                  (lob-draw (rest lob)))]))

;;Examples
(lob-draw empty)
(lob-draw (cons (make-ball 50 60 "red")
                (cons(make-ball 20 15 "blue")
                     (cons(make-ball 5 200 "black")empty))))

;;lob-member? LoB, ball -> boolean
;;Checks to see if the ball is in the list
(define (lob-member? lob b)
  (cond
    [(empty? lob) false]
    [(same-ball? b (first lob)) true]
    [else (lob-member? (rest lob) b)]))

;;Examples
(check-expect (lob-member? empty (make-ball 300 70 "blue"))
              false)
(check-expect (lob-member? (cons (make-ball 30 70 "blue")
                                 (cons (make-ball 310 500 "black")
                                   (cons (make-ball 30 340 "yellow") empty)))
                           (make-ball 310 500 "black")) true)

;;same-ball? ball ball -> boolean
;;Compares two balls
(define (same-ball? b1 b2)
  (and (= (ball-x b1) (ball-x b2))
       (= (ball-y b1) (ball-y b2))
       (string=? (ball-color b1) (ball-color b2))))
;;Example
(check-expect (same-ball? (make-ball 30 30 "white")(make-ball 30 30 "white"))
              true)
(check-expect (same-ball? (make-ball 30 30 "white")(make-ball 23 40 "black"))
              false)

Just a simple program where consume lists of balls, add them to empty scenes, count how many balls are on a given list, etc...

I've done everything but one thing. I have to design a function lob-yellow, which changes the color of all the balls in a list of Balls to yellow. I am guessing I need cond, but I am not sure how to. Any ideas?

Upvotes: 2

Views: 2245

Answers (2)

Óscar López
Óscar López

Reputation: 236034

Assuming that the struct is immutable, here are some hints to get you started, fill-in the blanks:

(define (lob-yellow lob)
  (cond [<???>                        ; if the list is empty
         <???>]                       ; return the empty list
        [else                         ; otherwise,
         (cons (make-ball             ; cons a new ball, build it with:
                (<???> (first lob))   ; the x coordinate of the first ball
                (ball-y <???>)        ; the y coordinate of the first ball
                <???>)                ; and always the yellow color
               (lob-yellow <???>))])) ; recur over the rest of the list

But if the struct were defined like this:

(define-struct ball (x y color) #:mutable) ; now the struct is mutable

... We could implement a solution that modifies each ball in the list in-place:

(define (lob-yellow lob)
  (cond [<???>                           ; if the list is empty
         <???>]                          ; return the empty list
        [else                            ; otherwise,
         (set-ball-color! <???> 'yellow) ; set the color of the first ball
         (lob-yellow <???>)]))           ; recur over the rest of the list

Upvotes: 2

soegaard
soegaard

Reputation: 31147

I have filled in a little of your template.

(define (yellow-to-blue lob)
  (cond
    [(empty? lob) ...]
    [else
     (cond
        [(symbol=? (first lob) 'yellow) 
         (cons ... (yellow-to-blue (rest lob)))]
        [else (cons ... (yellow-to-blue (rest lob)))])]))

Remember to write some test cases before you fill out the dots.

Upvotes: 1

Related Questions