我正在尝试通过 SICP 学习方案。练习 1.3 内容如下:定义一个过程,该过程将三个数字作为参数并返回两个较大数字的平方和。请评论我如何改进我的解决方案。
(define (big x y)
(if (> x y) x y))
(define (p a b c)
(cond ((> a b) (+ (square a) (square (big b c))))
(else (+ (square b) (square (big a c))))))
最佳答案
对我来说看起来还不错,您有什么具体要改进的吗?
你可以这样做:
(define (max2 . l)
(lambda ()
(let ((a (apply max l)))
(values a (apply max (remv a l))))))
(define (q a b c)
(call-with-values (max2 a b c)
(lambda (a b)
(+ (* a a) (* b b)))))
(define (skip-min . l)
(lambda ()
(apply values (remv (apply min l) l))))
(define (p a b c)
(call-with-values (skip-min a b c)
(lambda (a b)
(+ (* a a) (* b b)))))
这个 (proc p) 可以很容易地转换为处理任意数量的参数。
关于scheme - SICP 练习 1.3 征求意见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/161666/