(define (square n)
(* n n))
(define (fst-largest n1 n2 n3)
(cond ((and (>= n1 n2) (>= n1 n3)) n1)
((and (>= n2 n1) (>= n2 n3)) n2)
((and (>= n3 n1) (>= n3 n2)) n3)))
(define (scnd-largest nm1 nm2)
(if (>= nm1 nm2) nm1 nm2))
(define (sums x y z)
(define l1 (fst-largest x y z))
(define l2 (cond ((= l1 x) (scnd-largest y z))
((= l1 y) (scnd-largest x z))
((= l1 z) (scnd-largest x y))))
(+ (square l1) (square l2)))
lol thanks.
]]>(define (square n)
(* n n))
(define (fst-largest n1 n2 n3)
(cond ((and (> n1 n2) (> n1 n3)) n1)
((and (> n2 n1) (> n2 n3)) n2)
((and (> n3 n1) (> n3 n2)) n3)))
(define (scnd-largest nm1 nm2)
(if (>= nm1 nm2) nm1 nm2))
(define (sums x y z)
(define l1 (fst-largest x y z))
(define l2 (cond (= l1 x) (scnd-largest y z)
(= l1 y) (scnd-largest x z)
(= l1 z) (scnd-largest x y)))
(+ (square l1) (square l2)))
calling sums with 1 2 3 returns 10....now if my head is on right, shouldn't it return 13?
]]>(cond ((and (> x y) (> x z)) x) ...)
a new parenthesis.
but you could also use (max x (max y z)) to retrive the largest number (I guess there is a function named max in scheme).
btw.: I guess it would be easier to get the lowest number an just subtract it from the result.:
(-
(+ (sqr x) (sqr y) (sqr z))
(sqr
(min x
(min y z))
)
)
(define (sqr n)
(* n n))
(define (sum-of-squares x y z)
(define (largest-number (cond
(and (> x y) (> x z) x)
(and (> y x) (> y z) y)
(and (> z x) (> z y) z))))
(define (second-largest (cond
(and (= largest-number x)
(= largest-number y) (> y z) y)
(and (= largest-number y)
(= largest-number x) (> x z) x)
(if (= largest-number z)
(or (= largest-number z)
(> x y))
x
(or (= largest-number y)
(> y x))
y))))
(+ (sqr largest-number) (sqr second-largest)))
I know its horridly cumbersome right now, but i'm just learning the language and using only the few things the book teaches you before getting to this exercises. suggestions on fixing this procedure?
]]>