The sum of the squares of the tw

2015-04-15  本文已影响25人  zhizhuwang

SICP exercise 1.3

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))
)
上一篇 下一篇

猜你喜欢

热点阅读