Fun w/ lazy streams (was RE: functional programming)
Moshe Zadka
moshez at math.huji.ac.il
Sun Feb 27 00:42:14 EST 2000
Just one thing bugged me:
[Tim Peters]
> def sintersect2(s, t):
> """s, t -> stream intersection.
>
> s and t are increasing streams. Return the increasing
> stream containing the elements they have in common.
> """
>
> while 1:
> s1, t1 = s.head(), t.head()
> if s1 == t1:
> break
> elif s1 < t1:
> s = s.tail()
> else:
> t = t.tail()
> return Stream(s1, lambda s=s, t=t:
> sintersect2(s.tail(), t.tail()))
Tim, with the natural implementation of even_stream, and odd_stream,
you do realize (of course you do) that
sintersect2(even_stream, odd_stream)
is a good old fashioned infinite loop?
(What's more : sintersect2(even_stream, prime_stream) is not an infinite
loop, but taking its tail *is*)
I sort of wonder how common this bug is in Haskell: it doesn't *look* like
an infinite loop.
BTW: It reminded me of implementing the same thing in Scheme, where
everything is natural as breathing (with a snorkel, and through the ears,
but what the heck <wink>)
I actually think it was the same thing, except I didn't implement
intersect (probably unconcious fear of the infinite loop)
--
Moshe Zadka <mzadka at geocities.com>.
INTERNET: Learn what you know.
Share what you don't.
More information about the Python-list
mailing list