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
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/
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.
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
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.
(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
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,,,
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
> 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.
Seems likely to work. Permuting the arguments in
the recursive call is a clever idea. Clever ideas
usually require a comment.
Bear
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
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?
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
> 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)
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
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
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
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.