[racket] Help re-writing a function recursively

From: YC (yinso.chen at gmail.com)
Date: Tue Nov 16 13:30:11 EST 2010

On Tue, Nov 16, 2010 at 4:58 AM, Peter Breitsprecher
<pkbreits at lakeheadu.ca>wrote:

> I know I am missing something small, but I made that change, and it is
> terminating too soon, and not quite working correctly.  The nested list
> works fine, but the outer list isn't correct.
> Example.
> I give it input of '(3 (4 5) 6))
> and it returns
> (((4 5) 3))
>
> Which means it is terminating too soon, and it is reversing the inner list
> properly, but not the outer list after it completes that inner recursion.


Yes - as stated previously, you'll need to pass in the results from (cons
(read-list-helper '()) list-so-far) back to the helper in order not to
terminate early, and you should also check for the end of the file as well.

Cheers,
yc


>
> On 15 November 2010 21:13, YC <yinso.chen at gmail.com> wrote:
>
>>
>> Correct - that's the idea for reading in a nested list, unless you were
>> trying to flatten the list as you read them.
>>
>> The (cons (read-list-helper '() list-so-far) should also be passed again
>> to your helper proc, or it terminates prematurely.
>>
>> You might also want to add a test to see if you are at the end of the the
>> port, via eof-object?.
>>
>> Also - your chr is not used in your proc.  If that's the input-port you
>> will want to pass it to peek-char and read-char, otherwise they are read
>> from current-input-port.
>>
>> Let's add back the mailing list if you have additional questions, so
>> others might chime in.
>>
>> HTH.  Cheers,
>> yc
>>
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101116/9b536fdd/attachment.html>

Posted on the users mailing list.