Here's a version of your code that runs in the Intermediate Student
language ("local" isn't part of Beginning Student):

 (define (maxval lst)
   (local ((define (aux lst max)
             (cond [(null? lst)  max]
                   [(> (car lst) max) (aux (cdr lst) (car lst))]
                   [#t (aux (cdr lst) max)])))
     (aux lst 0)))

And here's the full grammar for ISL programs:
http://docs.racket-lang.org/htdp-langs/intermediate.html

Thanks for pointing out that the error message raised by `define` could be
improved.

On Mon, Feb 13, 2017 at 3:48 PM, Matthias Felleisen <matth...@ccs.neu.edu>
wrote:

>
> You may wish to read up on the design recipe and the necessity of ‘local’
> definitions in HtDP2e:
>
>   http://www.ccs.neu.edu/home/matthias/HtDP2e/
>
>
>
>
> > On Feb 13, 2017, at 3:46 PM, Angus Comber <anguscom...@gmail.com> wrote:
> >
> > But it has to have the signature maxval lst - it is a homework problem.
> >
> > So I thought I would need max in the helper function.  Basically to keep
> an internal max saved value.  Is there another way?
> >
> > On 13 February 2017 at 20:38, Matthias Felleisen <matth...@ccs.neu.edu>
> wrote:
> >
> > Just lift aux out.
> >
> >
> > > On Feb 13, 2017, at 3:18 PM, Angus Comber <anguscom...@gmail.com>
> wrote:
> > >
> > > If I change the code to:
> > >
> > >  (define (maxval lst)
> > >    (define (aux lst max)
> > >      (cond [(null? lst)  max]
> > >            [(> (car lst) max) (aux (cdr lst) (car lst))]
> > >            [#t (aux (cdr lst) max)]))
> > >    (aux lst 0))
> > >
> > > and comment out #lang racket at the top of the file and select
> Beginning Student as the language then I get error:
> > >
> > > define: expected only one expression for the function body, but found
> 1 extra part
> > >
> > > Is this because there is a 2nd define in the function?  Or do I need
> some syntax to indicate to run aux at the end of the definition.
> > >
> > > On 12 February 2017 at 21:34, Matthias Felleisen <matth...@ccs.neu.edu>
> wrote:
> > >
> > > > On Feb 12, 2017, at 3:00 PM, Angus <anguscom...@gmail.com> wrote:
> > > >
> > > > I am a beginner Racket developer by the way.
> > > >
> > > > Here is my maxval function which is supposed to take a list and
> return the largest integer in the list.
> > > >
> > > > (define (maxval lst)
> > > >   (define (aux lst max)
> > > >     (cond [(null? lst)  max]
> > > >           [(car lst) > max (aux (cdr lst) (car lst))]
> > > >           [#t (aux (cdr lst) max)]))
> > > >   (aux lst 0))
> > > >
> > > >
> > > > Using the function always returns the last value in the list
> > > >
> > > >> (maxval (list 1 2 3 5 6 1))
> > > > 1
> > > >
> > > > The code looks correct to me, but I must be missing something.  What
> have I done wrong?
> > >
> > >
> > > Your code is 100% correct, conceptually. Sadly, it’s suffering from a
> mistake that I saw many many times in the 90s and before. And that’s
> precisely why we get beginners started on Beginning Student Language in
> DrRacket instead of plain Racket. Try it out for your next few exercises or
> even this one, to see whether the error message would have helped.
> > >
> > > — Matthias
> > >
> > >
> >
> >
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to