SICP Exercise 1.3 request for comments

后端 未结 17 1645
耶瑟儿~
耶瑟儿~ 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条回答
  •  -上瘾入骨i
    2020-12-08 21:46

    Looks ok to me, is there anything specific you want to improve on?

    You could do something like:

    (define (max2 . l)
      (lambda ()
        (let ((a (apply max l)))
          (values a (apply max (remv a l))))))
    
    (define (q a b c)
      (call-with-values (max2 a b c)
        (lambda (a b)
          (+ (* a a) (* b b)))))
    
    (define (skip-min . l)
      (lambda ()
        (apply values (remv (apply min l) l))))
    
    (define (p a b c)
      (call-with-values (skip-min a b c)
        (lambda (a b)
          (+ (* a a) (* b b)))))
    

    And this (proc p) can be easily converted to handle any number of arguments.

提交回复
热议问题