SICP Exercise 1.3 request for comments

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

    I did it with the following code, which uses the built-in min, max, and square procedures. They're simple enough to implement using only what's been introduced in the text up to that point.

    (define (sum-of-highest-squares x y z)
       (+ (square (max x y))
          (square (max (min x y) z))))
    
    0 讨论(0)
  • 2020-12-08 21:33
    ;exercise 1.3
    (define (sum-square-of-max a b c)
      (+ (if (> a b) (* a a) (* b b))
         (if (> b c) (* b b) (* c c))))
    
    0 讨论(0)
  • 2020-12-08 21:34

    I think this is the smallest and most efficient way:

    (define (square-sum-larger a b c)
     (+ 
      (square (max a b))
      (square (max (min a b) c))))
    
    0 讨论(0)
  • 2020-12-08 21:35

    What about something like this?

    (define (p a b c)
      (if (> a b)
          (if (> b c)
              (+ (square a) (square b))
              (+ (square a) (square c)))
          (if (> a c)
              (+ (square a) (square b))
              (+ (square b) (square c)))))
    
    0 讨论(0)
  • 2020-12-08 21:37

    Using only the concepts presented at that point of the book, I would do it:

    (define (square x) (* x x))
    
    (define (sum-of-squares x y) (+ (square x) (square y)))
    
    (define (min x y) (if (< x y) x y))
    
    (define (max x y) (if (> x y) x y))
    
    (define (sum-squares-2-biggest x y z)
      (sum-of-squares (max x y) (max z (min x y))))
    
    0 讨论(0)
  • 2020-12-08 21:38

    I've had a go:

    (define (procedure a b c)
        (let ((y (sort (list a b c) >)) (square (lambda (x) (* x x))))
            (+ (square (first y)) (square(second y)))))
    
    0 讨论(0)
提交回复
热议问题