SICP 练习 1.3 征求意见 [英] SICP Exercise 1.3 request for comments

查看:48
本文介绍了SICP 练习 1.3 征求意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 SICP 学习方案.练习 1.3 内容如下:定义一个过程,该过程将三个数字作为参数并返回两个较大数字的平方和.请评论我如何改进我的解决方案.

I'm trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. Please comment on how I can improve my solution.

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

推荐答案

我觉得还行,您有什么具体要改进的地方吗?

Looks ok to me, is there anything specific you want to improve on?

你可以这样做:

(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) 可以很容易地转换为处理任意数量的参数.

And this (proc p) can be easily converted to handle any number of arguments.

这篇关于SICP 练习 1.3 征求意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆