How to detect finite/infinite lists?
Juanma Barranquero
lektu at terra.es
Thu Sep 18 21:16:33 EDT 2003
Extremely-newbie questions:
Is there any way to know if a list is finite or infinite, other than
doing:
length l
and waiting forever? :)
I ask because I was learning Haskell by writing some pretty naive
implementation of surreal numbers, where I used lists for left and right
surreal sets, and I wanted to do some operations on the sets (like
finding the maximum/minimum), but obviously just on finite sets.
I vaguely suspect the answer is going to be: "No, because lists are lazy
(at least when they are :) and there's no general way to know beforehand
how many elements they're going to have". But still, if I write
x = [1..5]
the compiler knows pretty well x is not going to grow any new member...
(Unrelated) Is there any standard function to do:
interleave [] _ = []
interleave _ [] = []
interleave (x:xs) (y:ys) = x : y : interleave xs ys
It's pretty easy to implement as shown or via zipWith, but given that
Haskell 98 already includes some "basic" functions (like cycle, iterate,
etc.) I wonder if I've missed this one somehow.
Thanks,
/L/e/k/t/u
More information about the Haskell-Cafe
mailing list