[racket] Help re-writing a function recursively

From: Peter Breitsprecher (pkbreits at lakeheadu.ca)
Date: Wed Nov 17 08:07:48 EST 2010

On 17 November 2010 03:04, YC <yinso.chen at gmail.com> wrote:

> On Tue, Nov 16, 2010 at 11:45 AM, Peter Breitsprecher <
> pkbreits at lakeheadu.ca> wrote:
>> Logically to me it would make sense, because what the value is returning
>> is list-so-far.
>> Which would turn into
>> (read-list-helper (cons list-so-far list-so-far))
>> then it would pass that newly constructed list to read-list-helper which
>> if it is another close bracket would return list-so-far.
>> Or is my logic flawed?
The value returned is not list-so-far, though you can say that it is the
> list-so-far from the nested read-list-helper.  But the result from the
> nested read-list-helper and the outer list-so-far are not the same list
> (i.e. they are not eq?), so it definitely is not (cons list-so-far
> list-so-far).

I knew that it was the result of the internal function call, but that it
would have been the value of that variable that is passed to it.  and the
other list so far would have been the stacked value  of list so far before
the internal function call.  However I still thought it should have worked

> On 16 November 2010 14:20, Peter Breitsprecher <pkbreits at lakeheadu.ca>wrote:
>> Then for the close brackets, It would just be [(eq? next-char #\))
>> (list-so-far)]?  I think don't think that is right but I haven't tried it
>> yet.
> list-so-far is a list, not a procedure and hence evaluating it (via putting
> it in the head position of the parentheses) will result in error.
> What you want is (reverse list-so-far) in this case.
I just wanted to return the value of list-so-far at that point, and I
assumed that was the way to do it.  Putting the reverse in still give me the
same errors

> It would be easier for you to see the behavior of the code if you have the
> compiler in front of you while you are asking the questions.  Also trace is
> your best friend for seeing the code visually, especially when dealing with
> recursions.   See
> http://docs.racket-lang.org/reference/debugging.html#(mod-path._racket/trace)<http://docs.racket-lang.org/reference/debugging.html#%28mod-path._racket/trace%29>for more details on trace.

I have had the comiler in front of me the past few times, but was trying to
understand the logic.  I haven't used trace, but I had been using debug so I
knew where it blew up, but not why.

> The value of keeping these conversations public is that others might be
> able to help answer your questions while I am away, in which case you are
> likely to get your answers earlier.   So please be sure to mail through the
> list going forward.
Sorry I just keep forgetting to reply all.

> Cheers,
> yc

Kurt Breitsprecher
(807) 474-9601
pkbreits at lakeheadu.ca
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101117/da80d73c/attachment.html>

Posted on the users mailing list.