2

我有这个问题。它很长。

首先,这是这个的数据定义

(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,但我不知道该怎么做。有任何想法吗?

4

2 回答 2

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
于 2013-02-13T00:01:03.330 回答
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)))])]))

请记住在填写点之前编写一些测试用例。

于 2013-02-12T23:35:41.530 回答