You are not logged in.

## #1 2005-09-16 07:04:16

dust
Member
Registered: 2005-06-04
Posts: 152
Website

### need some help with scheme

I think my logic's right, but I'm not sure where I'm going wrong here. Simple 2 procedures for an exercise (this isn't homework, I'm not in school just learning on my own) in the structured programming book. Basically need to write a function that takes 3 int and returns the sum of the 2 largest of the numbers squared.

``````(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?

Writing stories for a machine.

Offline

## #2 2005-09-16 12:17:05

cmp
Member
Registered: 2005-01-03
Posts: 350

### Re: need some help with scheme

try:

``(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))
)
)``````

Offline

## #3 2005-09-17 04:11:13

dust
Member
Registered: 2005-06-04
Posts: 152
Website

### Re: need some help with scheme

Ok, no more errors, now its just bugged lol. I can't see wear my logic is wrong (its 6:50am and the coffee here at work tastes like asswater).

``````(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?

Writing stories for a machine.

Offline

## #4 2005-09-17 13:49:10

cmp
Member
Registered: 2005-01-03
Posts: 350

### Re: need some help with scheme

I guess you have made the same mistake:
use (cond ((= l1 x) (scnd-largest y z)) .. instead of (cond (= l1 x) (scnd-largest y z) ..

Offline

## #5 2005-09-18 03:38:24

dust
Member
Registered: 2005-06-04
Posts: 152
Website

### Re: need some help with scheme

lol thanks.

Writing stories for a machine.

Offline

## #6 2005-09-18 06:38:28

cmp
Member
Registered: 2005-01-03
Posts: 350

### Re: need some help with scheme

no problem, I needed some time to figure this out, too.

Offline

## #7 2005-09-18 09:24:17

dust
Member
Registered: 2005-06-04
Posts: 152
Website

### Re: need some help with scheme

Finally working and appears to be bug free *pounds on wood*:

``````(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)))``````

Writing stories for a machine.

Offline