[racket] Help re-writing a function recursively

From: Peter Breitsprecher (pkbreits at lakeheadu.ca)
Date: Mon Nov 15 19:17:11 EST 2010

I have a program that is a simple reader.  Reads the user input character by
character, and then creates a list of values.  Right now it only works with
numbers, but i will add functionality for reading characters.  Right now it
will work, and just accept the numbers and create the list which is great,
but It won't work for nested lists at all.  I don't think it should be that
difficult to modify it, but it is proving to be harder than I anticipated.

(define (read-list chr)
  (define (read-list-helper list-so-far)
     (let ((next-char (peek-char))
           (this-char (read-char)))
           (cond [(char-numeric? next-char)(read-list-helper (cons this-char
list-so-far))]
            [(or (eq? next-char #\')
                 (eq? next-char #\))
                 (eq? next-char #\()
                 (eq? next-char #\space)) (read-list-helper list-so-far)]
            [(reverse list-so-far)])))
(read-list-helper '()))

The reading function passes it a character which I suppose it really doesn't
have to, however I thought to accept the nested list that I would need to
pass a list back into it when it read a "(".  I had tried passing the
list-so-far value to read-list, and modified the (read-list-helper '()) to
be (read-list-helper (list chr)) which did work kind of for input of (3 (4
5)), but nothing else.  Any ideas or suggestions?
-- 
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/20101115/b96806f1/attachment.html>

Posted on the users mailing list.