The sum of the squares of the two larger numbers

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))
)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,916评论 0 23
  • 第二十章 绚烂 天空的飞鸟斡旋在远方的山尖,痴痴地在寻找着什么,时而发出一点让人无法解释的孤鸣。难道这些小鸟也会学...
    川靈阅读 236评论 0 0
  • 脱离了KTV里嘈杂的环境,脱离了世俗的酒文化,带着几分醉意,坐在河边伴着徐来的清风,昏暗的月光,内心平静而杂...
    T伊恩阅读 285评论 0 1
  • 从未读过哈姆雷特,所以不知我心中是什么样的哈姆雷特,就像不知我心中旅行的意义是什么。 有人说越长大越孤单,越长大越...
    duXinG丶阅读 323评论 0 1