From: REALNAME
Subject: Please help (syntax problems?)
Date: 
Message-ID: <36D0789B.F06D62FD@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

From: Barry Margolin
Subject: Re: Please help (syntax problems?)
Date: 
Message-ID: <V8%z2.22$tR5.1816@burlma1-snr2>
In article <·················@top.cis.syr.edu>,
REALNAME  <········@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, ······@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.
From: charliew
Subject: Re: Please help (syntax problems?)
Date: 
Message-ID: <7aqd19$ob1$1@news.hal-pc.org>
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 <·················@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
>
From: Robert Monfera
Subject: Re: Please help (syntax problems?)
Date: 
Message-ID: <36D0EC7A.D1284BD2@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.
From: Christopher R. Barry
Subject: CAR, CADR, etc. vs FIRST, SECOND... (was Re: Please help (syntax problems?))
Date: 
Message-ID: <87zp66kf4f.fsf_-_@2xtreme.net>
Robert Monfera <·······@fisec.com> writes:

> 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.

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
From: Erik Naggum
Subject: Re: CAR, CADR, etc. vs FIRST, SECOND... (was Re: Please help (syntax problems?))
Date: 
Message-ID: <3128669611030356@naggum.no>
* ······@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
From: James A. Crippen
Subject: Re: CAR, CADR, etc. vs FIRST, SECOND... (was Re: Please help (syntax problems?))
Date: 
Message-ID: <678qvh1j.fsf@lambda.uaa.alaska.edu>
······@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--- ···@ ··@ Y+ PGP>+++ ···@ 5? X? R* !tv
b++++ DI++++ D--- G++>++++ e* h* r--- y-- NT{-} A48 HH++++>* PP+++
------END GEEK CODE BLOCK------
From: Erik Naggum
Subject: Re: CAR, CADR, etc. vs FIRST, SECOND... (was Re: Please help (syntax problems?))
Date: 
Message-ID: <3128952083733849@naggum.no>
* ········@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
From: charliew
Subject: Re: Please help (syntax problems?)
Date: 
Message-ID: <7at5oc$jvf$1@news.hal-pc.org>
Robert Monfera wrote in message <·················@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.