SICP Exercise 1.3

Exercise 1.3

(define (ex-1-3 a b c)
  (+ (square (big a b c))
     (square (medium a b c)))))

(define (big a b c)
  (cond ((and (>= a b) (>= a c)) a)
        ((and (>= b a) (>= b c)) b)
        (else c)))

(define (medium a b c)
  (cond ((or (and (>= a b) (<= a c)) 
             (and (<= a b) (>= a c))) a)
        ((or (and (>= b a) (<= b c)) 
             (and (<= b a) (>= b c))) b)
        (else c)))

8 Responses to SICP Exercise 1.3

  1. Anenome says:

    Wow, this one is a real brain teaser.

    The “Medium” test-case is quite tricky. Even after looking at your code I had difficulty grokking it. It looks like it’s using the exact opposite test here:

    ((or (and (>= a b) (<= a c))
    (and (= a c))) a)

    When I went back to look-up the exact rules for “or” and “and” I began to understand. The “or” definition choose the first correct “and” argument as the one to pass to the “cond”. I couldn’t understand why this was needed at first, until I wrote out the possible numerical variations and then wrote a test for each case, and when I’d done that I came out with the same code you had. It was pretty cool. Here’s the possible variations:

    a b c
    1 2 3 (>= b a) (= a b) (<= a c) "a"
    2 3 1 (= a c) “a”
    3 1 2
    3 2 1 )= b c) “b”

    Any set where C satisfies the middle-number condition can be relegated to the “else” case, so we didn’t construct a test for them, although we could have. But in any string of 3 numerals, eliminating all other numbers as the middle numeral makes the final number the correct value.

  2. Anenome says:

    Hmm, that came out garbled somehow. Let’s try to post the cases one more time:

    a b c
    1 2 3 (>= b a) (= a b) (<= a c) "a"
    2 3 1 (= a c) “a”
    3 1 2
    3 2 1 (= b c) “b”

  3. Anenome says:

    3rd try, and I’ll leave out the html that’s confusing the blog-software:

    a b c
    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1

    Those are the six cases you have to prepare for. We test for all cases except those in which C is the middle number (use “else”). The actual number values in the variable are unimportant, as any three non-equivalent numbers will satisfy the hierarchy test we’re creating.

    • dosimeta says:

      I am just reading through the book and am also solving the exercises to learn lisp. The solution i came up with seems a little shorter as it does not matter, which of the numbers a, b, c is the highest.
      The code below only checks for the number which we do not need for the calculation and uses the other two.

      (define (sum-sqr x y)
      (+ (square x) (square y)))

      (define (sum-squares-2-of-3 x y z)
      (cond ((and (<= x y) (<= x z)) (sum-sqr y z))
      ((and (<= y x) (<= y z)) (sum-sqr x z))
      ((and (<= z x) (<= z y)) (sum-sqr x y))))

  4. Marek says:

    (define (sum-square x y) (+ (* x x) (* y y)))

    (define (two-bigger-sum-square a b c)
    (if (> a b)
    (if (> b c)
    (sum-square a b)
    (sum-square a c))
    (if (> a c)
    (sum-square b a)
    (sum-square b c))))

  5. dimm says:

    (define (max x y)
    (if (> x y) x y))

    (define (square x) (* x x))

    (define (fun a b c)
    (if (> a b)
    (+ (square a) (square (max b c)))
    (+ (square b) (square (max a c)))))

    and tests

    (display (= 13 (fun 1 2 3)))
    (display (= 20 (fun 2 4 1)))
    (display (= 29 (fun 5 2 1)))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.