xsl-list
[Top] [All Lists]

Re: [xsl] Need help with XSLT tokenize

2020-08-13 06:02:22
What about XSLT 3's xsl:iterate? Isn't that, with its parameters that
can change on each iteration step, close to a procedural, sequential
loop?

Yes and no. Yes, it does kind of behave like a loop, and if you use it
to “loop over” a sequence, that’s one way to look at it. But no, it’s
not really like a for-loop in C or Python or your favorite imperative
language.

Functional languages often have things that are “loop like”: fold-left
and fold-right[1], for example (which XPath has), and more generally
recursive functions that can be implemented as loops by the compiler if
you structure them so that they’re tail recursive[2].

The xsl:iterate instruction is, from one perspective, a fold with a
syntax that makes it easier to understand. Or, as I described it
recently, a recursive function with guard rails (which is probably not
technically exactly right, but I claim poetic license).

Confession: I came relatively late to functional programming and I’ve
probably only used fold-left about a dozen times. Hard core functional
programmers will probably find that surprising as they’re really useful.
But if you don’t think in functional terms, they can really seem
pointless and confusing. My first actual use of fold-left happened
because I was tasked to maintain a bit of XQuery code where the previous
implementor had used an extension to mutate a variable in a loop. I
*knew* that it must be possible to do it without that horrible hack, and
I stared at until fold-left occurred to me *and* I worked out how to
make it work. About ten of the dozen times I’ve used fold left were
probably to solve exactly the same problem elsewhere in that code base.

                                        Be seeing you,
                                          norm

[1] https://en.wikipedia.org/wiki/Fold_(higher-order_function)
[2] https://en.wikipedia.org/wiki/Tail_call

--
Norman Tovey-Walsh <ndw(_at_)nwalsh(_dot_)com>
https://nwalsh.com/

As a general rule, the most successful man in life is the man who has
the best information.--Benjamin Disraeli
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

Attachment: signature.asc
Description: PGP signature

<Prev in Thread] Current Thread [Next in Thread>