Ah. So I instrumented your code with a at the beginning, and what I found was that in the recursive calls, the input was (
the first time and ((
the second time. So that's why you're getting those weird lists of parentheses.
If you just work with the text string, when you return from a recursive call, the outer call won't know how much of the string the inner call parsed. That's why I suggested a data structure that combines the string with a pointer to where you're up to in it:
The "2" in listify skips over the initial open paren. If the text doesn't start with an open paren, it doesn't represent a list, so I just report the same text.
This can, of course, be done purely functionally, but parsing is one of the things that turn out way easier non-functionally, because of this business of reading each character only once. You can think of text with pointer
as a sort of poor-man's object, with next character
as its only method.
I'm refraining from showing listify helper
so as not to spoil it if you want to write it yourself, but if you'd rather just read mine it's here: https://snap.berkeley.edu/snap/snap.html#present:Username=bh&ProjectName=listify
Once you've absorbed this technique, you can go back to parsing XML. :~)
P.S. Why not just
? Because TEXT is just a local variable in this block, and changing its value won't be visible to its caller. By contrast, REPLACE in a list
is visible to the caller, because the local variable and the caller's variable point to
the identical list. (But you could just put the text in a one-item list, and
next character
could replace the one item with its
all but first letter
.)
P.P.S. I have a slight bug in LISTIFY, which should skip over leading spaces before doing anything else. But you get the idea.