SICP Exercise 1.3 request for comments

后端 未结 17 1656
耶瑟儿~
耶瑟儿~ 2020-12-08 21:06

I\'m trying to learn scheme via SICP. Exercise 1.3 reads as follow: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two la

17条回答
  •  再見小時候
    2020-12-08 21:39

    Using only the concepts introduced up to that point of the text, which I think is rather important, here is a different solution:

    (define (smallest-of-three a b c)
            (if (< a b)
                (if (< a c) a c)
                (if (< b c) b c)))
    
    (define (square a)
            (* a a))
    
    (define (sum-of-squares-largest a b c) 
            (+ (square a)
               (square b)
               (square c)
               (- (square (smallest-of-three a b c)))))
    

提交回复
热议问题