Cartesian product of infinite lists in Haskell

Viewed 1495

I have a function for finite lists

> kart :: [a] -> [b] -> [(a,b)]
> kart xs ys = [(x,y) | x <- xs, y <- ys]

but how to implement it for infinite lists? I have heard something about Cantor and set theory.

I also found a function like

> genFromPair (e1, e2) = [x*e1 + y*e2 | x <- [0..], y <- [0..]]

But I'm not sure if it helps, because Hugs only gives out pairs without ever stopping.

Thanks for help.

3 Answers

you can think of the sequel as

0:          (0, 0)
           /      \
1:      (1,0)    (0,1)
       /     \  /     \
2:  (2,0)   (1, 1)   (0,2)
...

Each level can be expressed by level n: [(n,0), (n-1, 1), (n-2, 2), ..., (0, n)]

Doing this to n <- [0..]

We have

cartesianProducts = [(n-m, m) | n<-[0..], m<-[0..n]]
Related