2013-02-12 33 views
2

我遇到問題了。這很長。數據定義DrRacket?

首先,繼承人這個

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

我的繼承人計劃

(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) 

只是一個簡單的程序,其中消費球的名單,將它們添加到空的場景數據定義,算多少球是在給定的名單等...

我已經做了一切,但一件事。我必須設計一個函數lob-yellow,它將Balls列表中所有球的顏色更改爲黃色。我猜我需要cond,但我不知道如何。有任何想法嗎?

回答

2

假設結構是不可改變的,這裏有一些提示,讓你開始,填寫的空白:

(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 

但是,如果結構是這樣定義的:

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

。 ..我們可以實施一個解決方案,修改列表中的每個球:

(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 
1

我填寫了一點你的模板。

(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)))])])) 

記得在填寫點之前寫一些測試用例。