为什么《如何设计程序》这本书在他们的答卷上选择了这种方法?

我正在使用著名的《如何设计程序》一书。更具体地说,第一版(我有实体)。

在第6章中,有一些关于结构的练习。其中之一,您需要模拟交通灯并使用效果(突变)来改变它们。

我指的是在练习 练习6.2.5有关函数next是想给你红绿灯的一个颜色。

书中提供的答卷为:

(start 50 160)
(draw-solid-disk (make-posn 25 30) 20 'red)
(draw-circle (make-posn 25 80) 20 'yellow)
(draw-circle (make-posn 25 130) 20 'green)

; -------------------------------------------------------------------------

;; clear-bulb : symbol -> true
;; to clear one of the traffic bulbs
(define (clear-bulb color)
  (cond
    [(symbol=? color 'red) 
     (and (clear-solid-disk (make-posn 25 30) 20)
          (draw-circle (make-posn 25 30) 20 'red))]
    [(symbol=? color 'yellow) 
     (and (clear-solid-disk (make-posn 25 80) 20)
          (draw-circle (make-posn 25 80) 20 'yellow))]
    [(symbol=? color 'green)
     (and (clear-solid-disk (make-posn 25 130) 20)
          (draw-circle (make-posn 25 130) 20 'green))]))

;; tests
(clear-bulb 'red)

; -------------------------------------------------------------------------

;; draw-bulb : symbol -> true
;; to draw a bulb on the traffic light
(define (draw-bulb color)
  (cond
    [(symbol=? color 'red) 
     (draw-solid-disk (make-posn 25 30) 20 'red)]
    [(symbol=? color 'yellow) 
     (draw-solid-disk (make-posn 25 80) 20 'yellow)]
    [(symbol=? color 'green)
     (draw-solid-disk (make-posn 25 130) 20 'green)]))

;; tests
(draw-bulb 'green)

; -------------------------------------------------------------------------

;; switch : symbol symbol -> true
;; to switch the traffic light from one color to the next
(define (switch from to)
  (and (clear-bulb from)
       (draw-bulb to)))

;; tests
(switch 'green 'yellow)
(switch 'yellow 'red)

; -------------------------------------------------------------------------

;; next : symbol -> symbol
;; to switch a traffic light's current color and to return the next one  
(define (next current-color) 
  (cond 
    [(and (symbol=? current-color 'red) (switch 'red 'green)) 
     'green] 
    [(and (symbol=? current-color 'yellow) (switch 'yellow 'red)) 
     'red] 
    [(and (symbol=? current-color 'green) (switch 'green 'yellow)) 
     'yellow]))

(next 'red)
(next 'green)
(next 'yellow)
(next 'red)

在下一个函数中,我做了一个类似的事情,在提供的测试中取得了相同的结果:

(define (next current-color) 
  (cond 
    [(symbol=? current-color 'red) (switch 'red 'green)] 
    [(symbol=? current-color 'yellow) (switch 'yellow 'red)] 
    [(symbol=? current-color 'green) (switch 'green 'yellow)]))

与本书的答案不同,我的代码不使用and也不放过一个松散的单个符号(例如“红色”)。

这种差异引起了我的兴趣,因为这本书非常强调教您如何设计代码。令我感兴趣的一点是,原始解决方案使用and(组合后续效果),除了在每个条件语句的末尾使用“孤独”“红色”、“黄色”或“绿色”之外,这似乎是不必要的。

我不明白这最后一个符号语句或 and 的目的。

是否有一些风格或概念上的原因让这种方法看起来更冗长,更不清晰?

我读这本书正是为了改进我编写代码的方式。

回答

Racket 作为 Scheme 的一种,是一种面向表达式的语言。这意味着复合表达式中的最后一个表达式是整个表达式的value

这包括带引号的符号。它的值,即符号,是返回值。

函数调用会(next current-color)切换交通灯的颜色并返回一个指示交通灯新颜色的符号:

;; next : symbol -> symbol

您的代码切换颜色并返回true(根据 的规范switch):

;; switch : symbol symbol -> true
;; your-next : symbol -> true

这会改变函数的next使用方式。有了这本书的设计,我们可以写

....
   (let loop ( ... )
       .....
       (let ((current-color (next current-color)))
           ......
           ))
....

对于您的设计,这种自然风格的循环代码是不可能的。

一个普通的话:这些规范被称为类型,我们让各类指导我们的使用功能,在我们的代码。它们帮助我们看到什么进,什么出,所以我们可以连接匹配的电线,可以这么说。


以上是为什么《如何设计程序》这本书在他们的答卷上选择了这种方法?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>