Subject: Re: follow up to value of needed
From: (Rob Warnock)
Date: 6 May 2001 04:05:43 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9d2iin$5u5ei$>
Chez <> wrote:
|   This is what I have alrdy:
| (define (Max m lis)
|        (COND
|          ((NULL? lis) m)
|          ((< m (CAR lis)) (Max (CAR lis) (CDR lis)))
|          (ELSE (Max m (CDR lis)))
|        )
| )

O.k., looks like you're *almost* there, but... In Scheme and Lisp we
don't write "dangling parentheses" like that. Just put them all on the
last line of the thing they're closing (with no extra whitespace, either),
and you also don't need to indent the body so much:

  (define (max m lis)
    (cond ((null? lis) m)
	  ((< m (car lis)) (max (car lis) (cdr lis)))
	  (else (max m (cdr lis)))))

or (as some prefer):

  (define (max m lis)
      ((null? lis) m)
      ((< m (car lis)) (max (car lis) (cdr lis)))
      (else (max m (cdr lis)))))

| The dumb thing is I can get it to work unless I insert 0 to start it from
| from.  (max '0 '(1 2 3 4)) >> 4(highest val) anyone??

Just rename your "Max" to something like "max-helper", and then write a
new routine "max" that takes only one argument, and calls "max-helper"
with an initial guess. [Then -- *after* debugging it -- move "max-helper"
inside "max" using an internal define.]

But be careful about that initial guess! Zero is *not* always correct!
For example, what does your old "max" do when given this??

	(max 0 '(-17 -5 -12 -3 -9))

So think of another way to come up with an initial guess (one that's
*never* wrong).


Rob Warnock, 31-2-510
SGI Network Engineering		<URL:>
1600 Amphitheatre Pkwy.		Phone: 650-933-1673
Mountain View, CA  94043	PP-ASEL-IA