lolwut? There are exactly three cases. Use a cond structure to cover them. In b4 ``clever'' trickwankery.
Name:
Anonymous2008-01-07 0:02
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (square-largest x y z)
(cond (or (> x y) (> x z)) (if (and (> x y) (> x z)) (define a x) (define b x))
(or (> y x) (> y z)) (if (and (> y x) (> y z)) (define a y) (define b y))
(else (if (and (> z x) (> z y)) (define a z) (define b z))))
(sum-of-squares a b))
This is what I have.
Name:
Anonymous2008-01-07 0:05
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (square-largest x y z)
(cond (or (> x y) (> x z)) (if (and (> x y) (> x z)) (define a x) (define b x))
(or (> y x) (> y z)) (if (and (> y x) (> y z)) (define a y) (define b y))
(else (if (and (> z x) (> z y)) (define a z) (define b z))))
(sum-of-squares a b))
>>2
Actually, there's 13 cases:
x > y > z
x > z > y
y > x > z
y > z > x
z > x > y
z > y > x
(x = y) > z
(x = y) < z
(x = z) > y
(x = z) < y
(y = z) > x
(y = z) < x
x = y = z
>>8
Sage for noob faggotry. Please keep this shit off /prog/ thanks.
Name:
Anonymous2008-01-07 6:29
You guys should think simpler. If the procedure operates on the two higher values and there's three values, then it's easier to just check for the low value and operate on the other two.
>>15 >>11 (define *conses* '()) ; Global list to store keep-alive references to cons.
(define (better-cons x y) ; Please replace all instances of cons with better-cons.
(let ((m (cons x y)))
(set! *conses* (better-cons m *conses*))
m))
OP here, with the newfound simplicity in this exercise, I can change my code to
define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (square-largest x y z)
(cond (and (< x y) (< x z)) (define a y) (define b z)
(and (< y x) (< y z)) (define a x) (define b z)
(else (define a x) (define b y)))
(sum-of-squares a b))