SICP Exercise 1.3 request for comments

后端 未结 17 1680
耶瑟儿~
耶瑟儿~ 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:25

    Below is the solution that I came up with. I find it easier to reason about a solution when the code is decomposed into small functions.

                ; Exercise 1.3
    (define (sum-square-largest a b c)
      (+ (square (greatest a b))
         (square (greatest (least a b) c))))
    
    (define (greatest a b)
      (cond (( > a b) a)
        (( < a b) b)))
    
    (define (least a b)
      (cond ((> a b) b)
        ((< a b) a)))
    
    (define (square a)
      (* a a))
    

提交回复
热议问题