(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)))
This entry was posted on Friday, June 5th, 2009 at 12:35 pm and is filed under Scheme, SICP. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
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.
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.
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))))
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.
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”
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.
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))))
Your solution is better.
This is a very concise solution.
(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))))
(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)))