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

Please help (syntax problems?)

3 views
Skip to first unread message

REALNAME

unread,
Feb 21, 1999, 3:00:00 AM2/21/99
to
Hi everyone, i'm trying to do an assignment where i input 3 numbers and
output the sum of the squares of the 2 larger numbers. Here's my code,
i think there's something wrong with my paranthesis or something:

(define (sumofsquares a b c)
(if (and (> b a) (> c a)) (+ (sqrt b) (sqrt c))
(if (and (> a b) (> c b)) (+ (sqrt a) (sqrt c))
(else (+ (sqrt a) (sqrt b)))))

I don't know what is wrong with this definition, is it the parenthesis?
My book doesn't show me how to make multiple if statements in a
definition. An example of a definition with multiple if statements
would be helpfull too.

Thanks.


-Ivan


Barry Margolin

unread,
Feb 21, 1999, 3:00:00 AM2/21/99
to
In article <36D0789B...@top.cis.syr.edu>,

REALNAME <USER...@top.cis.syr.edu> wrote:
>Hi everyone, i'm trying to do an assignment where i input 3 numbers and
>output the sum of the squares of the 2 larger numbers. Here's my code,
>i think there's something wrong with my paranthesis or something:
>
>(define (sumofsquares a b c)
>(if (and (> b a) (> c a)) (+ (sqrt b) (sqrt c))
>(if (and (> a b) (> c b)) (+ (sqrt a) (sqrt c))
>(else (+ (sqrt a) (sqrt b)))))
>
>I don't know what is wrong with this definition, is it the parenthesis?

the "else" is incorrect; just put the expression you want. Also, your
parentheses aren't balanced -- you have too few close parens (although the
correct amount if you get rid of the "else").

>My book doesn't show me how to make multiple if statements in a
>definition. An example of a definition with multiple if statements
>would be helpfull too.

There's nothing special about multiple if expression. The then- or
else-part of an if expression can contain any expression, including another
if expression. It would help you to see the structure if you indented
reasonably.

(define (sumofsquares a b c)
(if (and (> b a) (> c a))
(+ (sqrt b) (sqrt c))
(if (and (> a b) (> c b))
(+ (sqrt a) (sqrt c))

(+ (sqrt a) (sqrt b)))))

However, typically a series of tests like this are written using a cond
expression rather than nested ifs:

(define (sumofsquares a b c)

(cond ((and (> b a) (> c a))
(+ (sqrt b) (sqrt c)))
((and (> a b) (> c b))
(+ (sqrt a) (sqrt c)))
(#t (+ (sqrt a) (sqrt b)))))

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

charliew

unread,
Feb 21, 1999, 3:00:00 AM2/21/99
to
This is probably too much help, but your problem could be solved much easier
by the following algorithm:

1) Sort numbers a, b, and c, in the order of largest to smallest
2) Sum the squares of the CAR of the sorted list and the CADR of the sorted
list

Try it ... you'll like it.

REALNAME wrote in message <36D0789B...@top.cis.syr.edu>...


>Hi everyone, i'm trying to do an assignment where i input 3 numbers and
>output the sum of the squares of the 2 larger numbers. Here's my code,
>i think there's something wrong with my paranthesis or something:
>
>(define (sumofsquares a b c)
>(if (and (> b a) (> c a)) (+ (sqrt b) (sqrt c))
>(if (and (> a b) (> c b)) (+ (sqrt a) (sqrt c))
>(else (+ (sqrt a) (sqrt b)))))
>
>I don't know what is wrong with this definition, is it the parenthesis?

>My book doesn't show me how to make multiple if statements in a
>definition. An example of a definition with multiple if statements
>would be helpfull too.
>

>Thanks.
>
>
>-Ivan
>

Robert Monfera

unread,
Feb 22, 1999, 3:00:00 AM2/22/99
to
charliew wrote:

> 1) Sort numbers a, b, and c, in the order of largest to smallest
> 2) Sum the squares of the CAR of the sorted list and the CADR of the sorted
> list

I would use FIRST and SECOND.

Christopher R. Barry

unread,
Feb 22, 1999, 3:00:00 AM2/22/99
to
Robert Monfera <mon...@fisec.com> writes:

One thing I dislike about using FIRST, SECOND and friends is if you
ever want something like CDADR then your code is kinda - I don't know
what the word I'm looking for is - "inconsistent" will have to
do. Using SECOND to take the CADR of a list and then taking the CDADR
of the same list just doesn't seem to be the most aesthetically
pleasing thing to do to me.

I often like to use symbols like FIRST and SECOND as the names of
variables within functions and it makes the code ever-so-slightly more
readable to not also use the FIRST and SECOND functions in conjuction
with these, IMO.

Christopher

Erik Naggum

unread,
Feb 22, 1999, 3:00:00 AM2/22/99
to
* cba...@2xtreme.net (Christopher R. Barry)

| One thing I dislike about using FIRST, SECOND and friends is if you ever
| want something like CDADR then your code is kinda - I don't know what the
| word I'm looking for is - "inconsistent" will have to do. Using SECOND to
| take the CADR of a list and then taking the CDADR of the same list just
| doesn't seem to be the most aesthetically pleasing thing to do to me.

think of FIRST and SECOND as accessors into the first level of a list.
think of CxR as accessors into a tree. if you see (rest (second X)) and
think CDADR is a better choice, you may need to think about what you're
representing. chances are a structure or class is a better choice than a
list, or that destructuring things before proceeding is more legible.

#:Erik

charliew

unread,
Feb 22, 1999, 3:00:00 AM2/22/99
to

Robert Monfera wrote in message <36D0EC7A...@fisec.com>...

>charliew wrote:
>
>> 1) Sort numbers a, b, and c, in the order of largest to smallest
>> 2) Sum the squares of the CAR of the sorted list and the CADR of the
sorted
>> list
>
>I would use FIRST and SECOND.

That's fine by me. I was arguing for a different approach rather than the
exact syntax of the code that should be used.

James A. Crippen

unread,
Feb 25, 1999, 3:00:00 AM2/25/99
to
cba...@2xtreme.net (Christopher R. Barry) writes:

> One thing I dislike about using FIRST, SECOND and friends is if you
> ever want something like CDADR then your code is kinda - I don't know
> what the word I'm looking for is - "inconsistent" will have to
> do. Using SECOND to take the CADR of a list and then taking the CDADR
> of the same list just doesn't seem to be the most aesthetically
> pleasing thing to do to me.

I quite agree. The one thing that really aggravated me about the Winston &
Horn Lisp book was that it used FIRST, REST and friends instead of CAR, ...
I had already had some experience with Scheme and Elisp so this really got
on my nerves. Other than that it was the best introductory Lisp book
that I'd seen and I used it as my primary reference until I obtained a
copy of CLtL2 a few years ago.



> I often like to use symbols like FIRST and SECOND as the names of
> variables within functions and it makes the code ever-so-slightly more
> readable to not also use the FIRST and SECOND functions in conjuction
> with these, IMO.

Hear, hear! Every time I try using FIRST or somesuch as a function arg
I get funny feelings about namespace problems and rename it. I know I
can get away with it but there's always the chance...

--
<crippenj at saturn math uaa alaska edu> Kallisti! <james at cryptology org>
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS/MU/O d?(--) s: a--(?) C+++(++++)>$ UBLOS*+++(++++)>$ P+>+++ L+++>$ E++++
W-- N++ o+ K+++>++++ w--- O- M- V--- PS+@ PE@ Y+ PGP>+++ t++@ 5? X? R* !tv
b++++ DI++++ D--- G++>++++ e* h* r--- y-- NT{-} A48 HH++++>* PP+++
------END GEEK CODE BLOCK------

Erik Naggum

unread,
Feb 25, 1999, 3:00:00 AM2/25/99
to
* crip...@saturn.math.uaa.alaska.edu (James A. Crippen)

| Hear, hear! Every time I try using FIRST or somesuch as a function arg I
| get funny feelings about namespace problems and rename it. I know I can
| get away with it but there's always the chance...

Common Lisp doesn't suffer from the namespace problems of Scheme, so the
issue simply doesn't arise if you use the symbols for variables.

#:Erik

0 new messages