Subject: Re: Just the first 'x' elements within a for-each
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Mon, 17 Mar 2003 19:56:25 +0100
|
Hi Jeni,
"Jeni Tennison" <jeni@xxxxxxxxxxxxxxxx> wrote in message
news:192796966587.20030317174203@xxxxxxxxxxxxxxxxxxx
> 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() <= $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
|