美文网首页
The sum of the squares of the tw

The sum of the squares of the tw

作者: zhizhuwang | 来源:发表于2015-04-15 09:44 被阅读25次

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.

bigger of two numbers

(define (bigger-of-two x y)
    (if (> x y) x
    y)

biggest of three numbers

(define (biggest-of-three x y z)
    (if (> (bigger-of-two x y) z) (bigger-of-two x y)
        z)
)

medium of three numbers

(define (medium-of-three x y z)
    (cond ((= (biggest-of-three x y z) x) (bigger-of-two y z))
             ((= (biggest-of-three x y z) y) (bigger-of-two x z))
             ((= (biggest-of-three x y z) z) (bigger-of-two y x)))
)

square of number

(define (square x) (* x x))

sum of two squares

(define (sum-of-squares x y)
    (+ (square x) (square y))
)

sum of squares of the two larger numbers

(define (fun x y z)
    (sum-of-squares (biggest-of-three x y z) (medium-of-three x y z))
)

相关文章

网友评论

      本文标题:The sum of the squares of the tw

      本文链接:https://www.haomeiwen.com/subject/qqcsfttx.html