xsl-list
[Top] [All Lists]

Re: Just the first 'x' elements within a for-each

2003-03-17 11:56:25
Hi Jeni,

"Jeni Tennison" <jeni(_at_)jenitennison(_dot_)com> wrote in message
news:192796966587(_dot_)20030317174203(_at_)jenitennison(_dot_)com(_dot_)(_dot_)(_dot_)
Hi Simon,

I've tried numerous connotations of for-each loops, recursive
functions etc but for the life of me I can't seem to get something
which will only print/process the first 'x' (in my case x is 2)
elements and then bail out of the for-each or just simply ignore the
remaining ones found.

In declarative programming, you can't "bail out".

Not true. This is possible if a recursively called template was used.

position() &lt;= $n

would be the "stop criterion" of the recursion.


It is also possible within an implementation of lazy evaluation.

E.g. in Haskell Prelude the function foldr is defined as follows:

foldr :: (a -> b -> b) -> b -> [a] -> b

foldr f z [] = z

foldr f z (x:xs) = f x (foldr f z xs)


Let f is defined like this:

f 0 y = 0
f x y = x * y

Then

foldr f  1 [1, 2, 3, 0, 4, 5, 6, 7, 8, 9, 10]

(this is the definition of the product of all numbers in a list)

will return the result

0

without ever going past the 4-th element of the list.

In this way we could operate even on lists of infinite length.



I have implemented a kind of lazy evaluation using FXSL and am currently
playing with it.

=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list