这个基本方案功能有什么问题
what's wrong with this basic scheme function
我正在尝试编写一个执行以下操作的方案函数(取自 SICP):
Exercise 1.3. Define a procedure that takes three numbers as arguments
and returns the sum of the squares of the two larger numbers.
这是我的尝试:
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c)))
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c)))
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
当我将其输入琵琶方案解释器时,我得到以下信息:
(square-sum-larger 4 5 6)
16
因为4
小于5
和6
,不应该计算第一个条件,意思是5
和[的平方和=14=]应该被退回?
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c))) ;; this is thrown away
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c))) ;; this is thrown away
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
三个中只有最后一个 cond
有用;前面的 cond
表达式没有任何副作用,因为它们只执行计算,并且不使用它们的值。这些是 Scheme 编译器可以完全消除的死代码。
您可能想将所有子句组合成一个条件:
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c))
((and (< b c) (< b a))) (+ (* a a) (* c c))
((and (< c a) (< c b))) (+ (* b b) (* a a))))
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c)))
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c)))
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
在JS中基本上是这样的:
function squareSumLarge(a, b, c) {
// not tail position , dead code regardless if the test is truthy
if (a < b && a < c) {
b * b + c * c;
}
// not tail position , dead code regardless if the test is truthy
if (b < c && b < a) {
a * a + c * c;
}
if (c < a && c < b0 {
// return because cond is in tail position
return b * b + a * a;
} else {
// if the last cond term didn't hit it returns an implementation chosen value
return undefined;
}
}
知道这个模式:
if (test1) {
return expression1;
} elseif (test2) {
return expression2;
} else {
return expression3;
}
当 cond
处于尾部位置时, 与 cond
是这样完成的:
(cond
(test1 expression1)
(test2 expression2)
(else expression3))
我正在尝试编写一个执行以下操作的方案函数(取自 SICP):
Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
这是我的尝试:
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c)))
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c)))
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
当我将其输入琵琶方案解释器时,我得到以下信息:
(square-sum-larger 4 5 6)
16
因为4
小于5
和6
,不应该计算第一个条件,意思是5
和[的平方和=14=]应该被退回?
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c))) ;; this is thrown away
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c))) ;; this is thrown away
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
三个中只有最后一个 cond
有用;前面的 cond
表达式没有任何副作用,因为它们只执行计算,并且不使用它们的值。这些是 Scheme 编译器可以完全消除的死代码。
您可能想将所有子句组合成一个条件:
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c))
((and (< b c) (< b a))) (+ (* a a) (* c c))
((and (< c a) (< c b))) (+ (* b b) (* a a))))
(define (square-sum-larger a b c)
(cond ((and (< a b) (< a c))) (+ (* b b) (* c c)))
(cond ((and (< b c) (< b a))) (+ (* a a) (* c c)))
(cond ((and (< c a) (< c b))) (+ (* b b) (* a a)))
)
在JS中基本上是这样的:
function squareSumLarge(a, b, c) {
// not tail position , dead code regardless if the test is truthy
if (a < b && a < c) {
b * b + c * c;
}
// not tail position , dead code regardless if the test is truthy
if (b < c && b < a) {
a * a + c * c;
}
if (c < a && c < b0 {
// return because cond is in tail position
return b * b + a * a;
} else {
// if the last cond term didn't hit it returns an implementation chosen value
return undefined;
}
}
知道这个模式:
if (test1) {
return expression1;
} elseif (test2) {
return expression2;
} else {
return expression3;
}
当 cond
处于尾部位置时, 与 cond
是这样完成的:
(cond
(test1 expression1)
(test2 expression2)
(else expression3))