SICP 练习 1.3 征求意见
我正在尝试通过 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))))))
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))))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我使用以下代码完成了此操作,该代码使用内置的
min
、max
和square
过程。 它们非常简单,只需使用到目前为止文本中介绍的内容即可实现。I did it with the following code, which uses the built-in
min
,max
, andsquare
procedures. They're simple enough to implement using only what's been introduced in the text up to that point.big
称为max
。 使用标准库功能。我的方法不同。 我没有进行大量测试,而是简单地将所有三个的平方相加,然后减去最小的一个的平方。
当然,您是否喜欢这种方法,或者一堆
if
测试,取决于您。使用 SRFI 95 的替代实现:
如上所述,但作为单行代码(感谢 synx @ freenode #scheme); 还需要 SRFI 1 和 SRFI 26:
big
is calledmax
. Use standard library functionality when it's there.My approach is different. Rather than lots of tests, I simply add the squares of all three, then subtract the square of the smallest one.
Whether you prefer this approach, or a bunch of
if
tests, is up to you, of course.Alternative implementation using SRFI 95:
As above, but as a one-liner (thanks synx @ freenode #scheme); also requires SRFI 1 and SRFI 26:
仅使用本书中该部分提出的概念,我会这样做:
Using only the concepts presented at that point of the book, I would do it:
像这样的事怎么办?
What about something like this?
仅使用文本中介绍的概念,我认为这相当重要,这是一个不同的解决方案:
Using only the concepts introduced up to that point of the text, which I think is rather important, here is a different solution:
我认为这是最小且最有效的方法:
I think this is the smallest and most efficient way:
以下是我想出的解决方案。 我发现当代码被分解为小函数时,更容易推理出解决方案。
Below is the solution that I came up with. I find it easier to reason about a solution when the code is decomposed into small functions.
很高兴看到其他人如何解决这个问题。 这是我的解决方案:
我通过迭代解决了它,但我更喜欢 ashitaka 和 (+ (square (max xy)) (square (max (min xy) z))) 解决方案,因为在我的版本中,如果 z 是最小的数,是更大的吗? 被调用两次,造成不必要的缓慢和迂回的过程。
It's nice to see how other people have solved this problem. This was my solution:
I solved it by iteration, but I like ashitaka's and the (+ (square (max x y)) (square (max (min x y) z))) solutions better, since in my version, if z is the smallest number, isGreater? is called twice, creating an unnecessarily slow and circuitous procedure.
这是另一种方法:
示例:
Here's yet another way to do it:
Example:
您还可以对列表进行排序并添加排序列表的第一个和第二个元素的平方:
You can also sort the list and add the squares of the first and second element of the sorted list:
在 Scott Hoffman 和一些 irc 的帮助下,我纠正了我的错误代码,如下
With Scott Hoffman's and some irc help I corrected my faulty code, here it is
我已经尝试过了:
I've had a go:
我觉得还不错,您有什么具体需要改进的地方吗?
你可以这样做:
这个 (proc p) 可以很容易地转换来处理任意数量的参数。
Looks ok to me, is there anything specific you want to improve on?
You could do something like:
And this (proc p) can be easily converted to handle any number of arguments.