[stringtemplate-interest] "rest" and template recursion

Sasha Crosland sasha.crosland at gmail.com
Fri May 30 23:05:22 PDT 2008

Thanks for the explanation, Terence.  A side-effect-free rest() does seem
more in the spirit of StringTemplate, doesn't it?  I vote yes, for what it's

But would you actually need to make a copy of the list data?  If lists are
immutable within a template, then couldn't you easily return a lightweight
view of the sublist that reads through to the original list, just like with
an iterator?

On Fri, May 30, 2008 at 10:28 PM, Terence Parr <parrt at cs.usfca.edu> wrote:

> Yeah, I use an iterator rather than making a copy of the list, which of
> course causes trouble with recursion.  rest() returns something with a
> side-effect...seems you're right...perhaps we should make it copy the list
> (well, minus first element).
> Anybody vote yes or no?
> Ter
> On May 30, 2008, at 8:22 PM, Sasha Crosland wrote:
>  Hi everyone,
>> I had been having difficulty getting template recursion to work as I had
>> hoped it would, and when I searched briefly for anything addressing this
>> topic in the list archives, I found that others had apparently had a similar
>> problem to mine.  From what I gathered, the problem is not to do with
>> recursion itself, but with use of the "rest" operator on a multi-valued
>> attribute, which I have been trying to use in the implementation of my
>> recursive template.  Something to do with "rest" returning an iterator
>> instead of a list?
>> What I want to do is to generate the names of Java types such as "Fn<P1,
>> Fn<P2, Fn<P3, R>>>," which I am using as a "curried" representation of a
>> three-argument function with type P1 x P2 x P3 -> R.  I tried to define a
>> template called curriedFnType taking a single attribute called arity, which
>> is a list of integers such that arity[i] = i for i from 1 to length(arity).
>> curriedFnType(arity) ::= <<
>> <if(arity)>
>> Fn\<P<first(arity)>, <curriedFnType(arity=rest(arity))>\>
>> <else>
>> R
>> <endif>
>> >>
>> The problem is that for an arity of four ([1,2,3,4]), for example, the
>> template is instantiated to "Fn<P1, Fn<P2, Fn<P4, R>>>", which somehow skips
>> over P3.  I encounter similar quirkiness with whatever sort of recursive
>> template I try to implement in this manner.  And I don't think that using
>> the colon operator to apply a template to the multi-valued arity attribute
>> will work directly, will it?, since we aren't dealing with tail-recursion.
>>  The alternative
>> curriedFnType2(arity) ::= "<arity:{Fn\<P<it>, }>R<arity:{\>}>
>> seems to work, but, to be honest, it feels rather lame that I should have
>> to resort to this.  Can anybody shed any light on the issue?  Is this indeed
>> a bug, or am I overlooking something?  I am using StringTemplate 3.1.
>> Sasha
>> _______________________________________________
>> stringtemplate-interest mailing list
>> stringtemplate-interest at antlr.org
>> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080530/d42e2df3/attachment.html 

More information about the stringtemplate-interest mailing list