[newbie] Lazy >>= ?!

andrew@andrewcooke.free-online.co.uk andrew@andrewcooke.free-online.co.uk
Fri, 16 Feb 2001 22:19:27 +0000

There seems to be something funny going on here - I thought all the
following were equivalent, but only listList1 works with
> liftM (take 2) listListn

Is there any way to write listList2 or 3 so that they are lazy (what
I'm really trying to do is get the same kind of code to work on an IO
Monad that contains an infinite list, but when that failed I wondered
why it worked for a list monad containing an infinite list and then
managed to get that not to work either...)

Thanks, Andrew (code follows)

listList1 :: [[Integer]]
listList1 = [x] where x = 1:x

listList2 :: [[Integer]]
listList2 = do rest <- listList2
               return (1:rest)

listList3 :: [[Integer]]
listList3 = listList3 >>= (\x -> return (1:x))