Re: Fast expanding macros /Was: How to declare recursive factorial function?



Le (on) vendredi 14 mars 2008 21:47, Ulrich Diez a écrit (wrote) :

There is plenty of stuff where expansion is not suppressed in
order to obtain arguments, values or whatever.
E.g., expansion takes place when looking for opening-braces or
key-words related to things like register-assignments or
\unexpanded or \scantokens, with those \if-comparisons, with
\csname, with \the, when gathering a numerical value, at some
places within alignments...

Ok. I realise I wasn't so clear about my question. Actually, it was about
when we can use those tricks (however, I didn't knew the whole list, for
example I'd never thought to things like \toks0 or \unexpanded, so thanks
for all this ideas). So, perhaps more clearly stated, the question is:

Given a macro that works purely by expansion, is there always a way to make
it deliver its result in only two expansion steps (that is, with only 3
\expandafter)? If no, what are the limits?

For some obscure reason I like \romannumeral.
This can also be used for argument-list-iterating.

Wow. Frankly, I wasn't sure this was possible. I'll have to study your code
below carefuly.

E.g., create an iterator for the list which besides the list
takes some arguments used for action after recursion.
Let the iterator modify these arguments during iteration...

e.g. \iterate{foo}{elem1,elem2,...}
next step: \iterate{baz}{elem2,...}

...while not delivering any token "outside" the iterator's
"argument-grabbing-range".

This is it. I missed that point.

At the end of the recursion, have the iterator return the
modified argument.

I'll have to read this carefuly in your code too, because it isn't that
clear to me...

But comma-list-parsing is poorly implemented (e.g., no lead-and-
trail-space-checking, also no check for a missing comma at the
end of the list.)

No matter, that's not exactly the point here.

Actually I don't like comma-lists all too much any more and
prefer "argument-lists" wherever possible because with comma-
lists you always need to decide upon the treatment of "edge-
cases", e.g. consecutive commas, trailing-comma, leading-comma,
only blank-stuff between comma, how many levels of brace-
stripping, catcode-change of comma, ...

Hmm, I think I agree with you. I always have a bad time thinking about these
problems (esp. the trailing coma at end) when manipulating coma-separated
lists. I wonder why I didn't switch to argument-list more often...

If you are interested in such stuff, have a look at the
TeX pearl divers site at http://www-stary.gust.org.pl/pearls/
(-> Pearls 2005 -> David Kastrup: Iterating with roman numeral).

Unfortunetaly, I've got a 404 code right now with this address. Hope it is
only a temprary errer.

I also learned a lot when Robin Fairbairns pointed me to the
'Around the bend'-department at
http://www.ctan.org/tex-archive/info/aro-bend/.
There you find the text-files exercise.001 - exercise.018
and answer.001 - answer.018.

Thanks for the hint. I saw that a while ago, but wasn't quite ready for all
the exercises. Maybe I'll have another look at it now.

\documentclass{article}
\makeatletter

By the way, I'll probably need some time to read your code. Maybe I'll come
up with a few question in some time...

Thanks for all your explanations !

Manuel.
.



Relevant Pages