SICP Exercise 1.3 request for comments

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

    It's nice to see how other people have solved this problem. This was my solution:

    (define (isGreater? x y z)
    (if (and (> x z) (> y z))
    (+ (square x) (square y))
    0))
    
    (define (sumLarger x y z)
    (if (= (isGreater? x y z) 0)   
    (sumLarger y z x)
    (isGreater? x y z)))
    

    I solved it by iteration, but I like ashitaka's and the (+ (square (max x y)) (square (max (min x y) z))) solutions better, since in my version, if z is the smallest number, isGreater? is called twice, creating an unnecessarily slow and circuitous procedure.

提交回复
热议问题