Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scheme Projects

159 views
Skip to first unread message

acutekitten

unread,
Sep 4, 2004, 12:50:18 PM9/4/04
to
I am trying to learn scheme and having some trouble with some of the
exercises from a book. The Exercise is as follows: "Define a procedure
that takes three numbers as arguments and retruns the sum of the squares
of the two larger numbers." What is this problem exactly looking for? (* 3
4 5) then (+ (* 4 4)(* 5 5))? does it want me to pick the numbers or does
it want them to be varriables or what?

Joe Marshall

unread,
Sep 4, 2004, 12:56:35 PM9/4/04
to
"acutekitten" <joeybut...@yahoo.com> writes:

It wants you to write a procedure:

(define sum-of-squares-of-largest
(lambda (x y z) ...your code here...))

or, depending on the book,

(define (sum-of-squares-of-largest x y z)
...your code here...)

So that you can call

=> (sum-of-squares-of-largest 4 2 5)

and get the answer 41

--
~jrm

acutekitten

unread,
Sep 4, 2004, 4:48:25 PM9/4/04
to
Forgive my ignorance, i kinda know how to define one such as ((> x 0) x)
but that is about it so i really don't know what to put into the
parentesies.

Neil W. Van Dyke

unread,
Sep 4, 2004, 8:44:30 PM9/4/04
to ne...@neilvandyke.org

I think you'll find the "How To Design Programs" book very helpful:
http://www.htdp.org/

As they say: Don't rush. Start at the beginning, and work through
each section at your own pace.

For best use of the book, you'll want to install DrScheme:
http://www.drscheme.org/

acutekitten

unread,
Sep 5, 2004, 2:18:48 PM9/5/04
to
i honestly don't know what to put in this part (define

sum-of-squares-of-largest
(lambda (x y z) ...your code here...))

If someone could just fill in the whole problem i could see then what to
do and what i am missing.
The book im learning out of is called: "Structure and Interpretation of
Computer Programs"
by Harold Abelson
and Gerald Jay Sussman
with Julie Sussman

Joe Marshall

unread,
Sep 5, 2004, 2:29:42 PM9/5/04
to
"acutekitten" <joeybut...@yahoo.com> writes:

That's the one I learned from.

I really don't want to give too much away here, so let's start slowly.

(define (sum-of-squares-of-largest x y z)

... your code here ...)

The first thing you need to do is determine which two of x, y, and z
are the largest. A COND expression should do the trick.

There are three cases, x and y are largest, x and z are largest, and y
and z are largest. Your COND expression should therefore have three
cases.

Once you know which two are the largest, you need to square those two
and add the results.

--
~jrm

Abdulaziz Ghuloum

unread,
Sep 5, 2004, 3:09:19 PM9/5/04
to
Joe Marshall wrote:
> "acutekitten" <joeybut...@yahoo.com> writes:
>
>
>>i honestly don't know what to put in this part (define
>>sum-of-squares-of-largest
>> (lambda (x y z) ...your code here...))
>>
>>If someone could just fill in the whole problem i could see then what to
>>do and what i am missing.
>>The book im learning out of is called: "Structure and Interpretation of
>>Computer Programs"
>>by Harold Abelson
>>and Gerald Jay Sussman
>>with Julie Sussman
>
>
> That's the one I learned from.
>
> I really don't want to give too much away here, so let's start slowly.
>
> (define (sum-of-squares-of-largest x y z)
>
> ... your code here ...)
>
> The first thing you need to do is determine which two of x, y, and z
> are the largest. A COND expression should do the trick.

If I may add a couple of [trivial?] hints: Use (> x y) to test if x is
greater than y. Use (and test1 test2) to decide if both test1 and test2
are true.

> There are three cases, x and y are largest, x and z are largest, and y
> and z are largest. Your COND expression should therefore have three
> cases.

You can also think about it as: if z is the smallest, then x and y are
the largest. You have three cases in your cond expression: x is the
smallest, y is the smallest, or z is the smallest.

> Once you know which two are the largest, you need to square those two
> and add the results.

To square a number x, you can multiply it by itself.

Andreas Krey

unread,
Sep 6, 2004, 1:57:40 PM9/6/04
to
* Abdulaziz Ghuloum (aghu...@c-s-remove-dashes.indiana.edu)
....

> You can also think about it as: if z is the smallest, then x and y are
> the largest. You have three cases in your cond expression: x is the
> smallest, y is the smallest, or z is the smallest.
>
Funnily, I come up with a binary decision tree:

(if (< a b)
(if (< a c)
(sq b c)
(sq a b))
(if (< b c)
(sq a c)
(sq a b)))

and I don't see how to put this into a single (three-case) cond without
increasing the number of comparisons done.

Andreas

--
np: 4'33

Abdulaziz Ghuloum

unread,
Sep 6, 2004, 2:51:01 PM9/6/04
to
Andreas Krey wrote:


I didn't say that you can do it in three-case cond without increasing
the number of comparisons done. The three-case cond is optimized for
readability since it does not require any thinking to verify that it works.

Aziz,,,

Kristof Bastiaensen

unread,
Sep 6, 2004, 4:07:02 PM9/6/04
to

Here is an alternative way to the ones already proposed:
You can define a auxiliary function sum-of-sq-aux, that takes
three arguments (x, y and z), but requires the first two to be
in order (< x y). This way you know that y will be part of the
calculation. Now you only need to find out which is the other
(x or z), and perform the calculation. From your main function
you call the auxiliary, with the two first elements put in order.
If possible, put this auxiliary function inside the definition of
the main one.

Regards,
KB

Bruce Lewis

unread,
Sep 7, 2004, 9:09:17 AM9/7/04
to
"acutekitten" <joeybut...@yahoo.com> writes:

> If someone could just fill in the whole problem i could see then what to
> do and what i am missing.
> The book im learning out of is called: "Structure and Interpretation of
> Computer Programs"
> by Harold Abelson
> and Gerald Jay Sussman
> with Julie Sussman

Since school is starting up, you're going to encounter a lot of
resistance. You might be asking us to do your homework problems for us,
and folks expect you'll be better off doing your own homework.

The homework probably expects you to make use of something you just
learned. Look through the material associated with the homework and try
to use that. Asking on the newsgroup is likely to get you a solution
that's useless for learning material, e.g.

(define (f a b c)
(if (= a (min a b c))
(+ (* b b) (* c c))
(f b c a)))

The above solution will work, but it probably won't teach you anything
that will help on your first quiz.

acutekitten

unread,
Sep 7, 2004, 11:16:51 PM9/7/04
to
(define (sum-of-squares-of-largest x y z)
(define (f x y z)
(if (= x (min x y z))
(+ (* y y) (* z z))
(f y z x)))
so is this right then so far?

Ray Dillinger

unread,
Sep 8, 2004, 1:03:58 AM9/8/04
to


Seems likely to work. Permuting the arguments in
the recursive call is a clever idea. Clever ideas
usually require a comment.

Bear

Jens Axel Søgaard

unread,
Sep 8, 2004, 2:42:29 AM9/8/04
to

I'd say it is cheating. At this point SICP hasn't
mentioned the function min.

Use the hints given previously about what to use
as tests in a cond-expression.

(don't despair - this exercise is harder than it seems at first)

--
Jens Axel Søgaard

Joe Marshall

unread,
Sep 8, 2004, 1:40:50 PM9/8/04
to
"acutekitten" <joeybut...@yahoo.com> writes:

Let me do a related example.

(define (sum-of-smallest x y z)
(cond ((and (< x z) (< y z)) (+ x y))
((and (< x y) (< z y)) (+ x z))
;; no need to check if x is largest at this point.
(else (+ y z))))

(sum-of-smallest 3 4 5)
=> 7

(sum-of-smallest 10 4 2)
=> 6


Now what would you change to make it compute the sum of the squares of
the largest?

acutekitten

unread,
Sep 8, 2004, 3:18:17 PM9/8/04
to
(define (sum-of-squares-of-largest x y z)
(cond (and ((< a b)(< a c)(+ (* b b)(* c c))))
((< b c)(< b a)(+ (* a a)(* c c))))
((< c a)(< c b)(+ (* a a)(* b b))))))
what am i missing here it won't run?

Jens Axel Søgaard

unread,
Sep 8, 2004, 3:38:30 PM9/8/04
to
acutekitten wrote:

Let's write Joe's function with square brackets, so
it is easier to discern the clauses in the cond-expression:

(define (sum-of-smallest x y z)
(cond
[(and (< x z) (< y z)) (+ x y)]
[(and (< x y) (< z y)) (+ x z)]
[else (+ y z)]))

The syntax of a cond-expression is

(cond
[<question1> <answer1>]
[<question2> <answer2>]
[else <default-answer>])

The question you want to ask in the first clause is:

Is both (< a b) and (< a c) true?

This is written as:

(and (< a b) (< a c) )

Thus your first clause becomes:

(define (sum-of-squares-of-largest x y z)
(cond

[(and (< a b) (< a c)) (+ (* b b) (* c c))]
...


--
Jens Axel Søgaard

Jens Axel Søgaard

unread,
Sep 8, 2004, 3:44:58 PM9/8/04
to
Jens Axel Søgaard wrote:

> acutekitten wrote:
>
>> (define (sum-of-squares-of-largest x y z)
>> (cond (and ((< a b)(< a c)(+ (* b b)(* c c))))
>> ((< b c)(< b a)(+ (* a a)(* c c))))
>> ((< c a)(< c b)(+ (* a a)(* b b))))))
>> what am i missing here it won't run?

> Thus your first clause becomes:
>
> (define (sum-of-squares-of-largest x y z)
> (cond
> [(and (< a b) (< a c)) (+ (* b b) (* c c))]
> ...

Make that

(define (sum-of-squares-of-largest a b c)

acutekitten

unread,
Sep 8, 2004, 4:35:08 PM9/8/04
to
(define (sum-of-squares-of-largest x y z)

Ray Dillinger

unread,
Sep 8, 2004, 6:24:29 PM9/8/04
to

The way your editor indented it should be a clue.
Ask yourself... how many times did you mean "and"?
now, how many times did you say it?

Look carefully at the cond clause syntax in the
example code in your book.

Bear

acutekitten

unread,
Sep 8, 2004, 8:02:19 PM9/8/04
to
so it should be:

(define (sum-of-squares-of-largest x y z)
(cond (and ((< a b)(< a c)(+ (* b b)(* c c)))
((< b c)(< b a)(+ (* a a)(* c c)))

acutekitten

unread,
Sep 8, 2004, 9:19:08 PM9/8/04
to
(define (sum-of-largest x y z)
(cond
((and (< x y)(< x z))(+ (* y y)(* z z))
((and (< y x)(< y z))(+ (* x x)(* z z))
(else (+ (* x x)(* y y))))


Ray Dillinger

unread,
Sep 8, 2004, 9:30:05 PM9/8/04
to


No, it should not. You intended to use "and"
three times, am I right? In each of three different
clauses, you want to check for two different things
both being true.

What you wrote above only mentions "and" once.

The way "cond" works, you need one clause for
each different thing it can do. Each clause is
an open paren, then a check expression, then
a return expression, then a close paren. So,
for example,

(cond ( #f 23)
( #t 12) )

should return 12, because 12 is the return expression
of the first cond clause whose check expression was
true.

Now, you're trying to build a cond statement with
three clauses here; think about the structure of
each clause. Which part is the check expression
and which part is the return expression?

You should be able to type the expressions at the
REPL to see what cond is looking at. It will help.

Bear


Phil Bewig

unread,
Sep 9, 2004, 11:57:06 AM9/9/04
to
"acutekitten" <joeybut...@yahoo.com> wrote in message news:<d0dd25fa8e48c12c...@localhost.talkaboutprogramming.com>...

After fixing the missing parentheses, it looks like this:

(define (sum-of-largest x y z)
(cond

((and (< x y)(< x z))(+ (* y y)(* z z)))
((and (< y x)(< y z))(+ (* x x)(* z z)))
(else (+ (* x x)(* y y)))))

Which is still wrong. Consider:

> (sum-of-largest 3 4 5)
41
> (sum-of-largest 4 4 4)
32
> (sum-of-largest 3 4 4)
32
> (sum-of-largest 3 3 4)
18 =======================> should be 25
> (sum-of-largest 3 3 2)
18
>


Phil

Joe Marshall

unread,
Sep 9, 2004, 12:09:09 PM9/9/04
to
"acutekitten" <joeybut...@yahoo.com> writes:

Very close. Notice this, though:


Where is the matching close paren for this?
v


> ((and (< x y)(< x z))(+ (* y y)(* z z))


Once you fix that, you'll be all set.

Joe Marshall

unread,
Sep 9, 2004, 12:12:35 PM9/9/04
to
Joe Marshall <j...@ccs.neu.edu> writes:

Except for the bug that Phil Bewig noted. argh!

0 new messages