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))
)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,134評論 0 23
  • 第二十章 絢爛 天空的飛鳥斡旋在遠方的山尖,癡癡地在尋找著什么,時而發(fā)出一點讓人無法解釋的孤鳴。難道這些小鳥也會學(xué)...
    川靈閱讀 298評論 0 0
  • 脫離了KTV里嘈雜的環(huán)境,脫離了世俗的酒文化,帶著幾分醉意,坐在河邊伴著徐來的清風(fēng),昏暗的月光,內(nèi)心平靜而雜...
    T伊恩閱讀 322評論 0 1
  • 從未讀過哈姆雷特,所以不知我心中是什么樣的哈姆雷特,就像不知我心中旅行的意義是什么。 有人說越長大越孤單,越長大越...
    duXinG丶閱讀 360評論 0 1

友情鏈接更多精彩內(nèi)容