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

问题描述

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

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

我指的是 exercise 练习 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,是一种 expression-oriented language。这意味着复合表达式中的最后一个表达式是整个表达式的

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

函数调用 (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)))
           ......
           ))
....

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

一般 备注:这些规范被称为类型,我们让类型指导我们在代码中使用函数。它们可以帮助我们查看输入和输出内容,以便我们可以连接匹配的电线