Subject: Re: need help writing function to find largest value of a list!
From: rpw3@rigden.engr.sgi.com (Rob Warnock)
Date: 5 May 2001 04:12:13 GMT
Newsgroups: comp.lang.scheme
Message-ID: <9cvuit$5ndia$1@fido.engr.sgi.com>
Chez <yuugy09@bellsouth.net> wrote:
+---------------
|    I'm trying to write a function to find the largest number from a given
| list.  I know Scheme has a function max that gives the max back.  I dont
| want to use that but make the function without using it.  Any pointers??
+---------------

First, write your own "max" that works with just two arguments.

Given that, what could you do if someone handed you number which was
a guess at the maximum, plus a list of possibly-larger values? Well...

- If the list is empty, your guess is as good as it gets, so return that.
  All done.

- If the list isn't empty, then the first element on the list is either
  bigger than the current guess or not, so your new guess is the maximum
  of your current guess and said first element. Now figure out what to do
  with that new guess and the rest of the list...


-Rob

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