[newbie] Lazy >>= ?!
andrew@andrewcooke.free-online.co.uk
andrew@andrewcooke.free-online.co.uk
Sat, 17 Feb 2001 13:25:30 +0000
Thanks, that was very helpful, but it does raise two further questions
(neither of which is critical, to my program, but if anyone cares to
comment I'm interested).
1. After digesting what you wrote I managed to make a lazy list of IO
monads containing random numbers, but couldn't make an IO monad that
contained a lazy list of random numbers. Is this intentional, me
being stupid, or just chance?
My code for producing [IO ..] is:
listParams :: [IO (Point, Double)]
listParams = entry : listParams
entry :: IO (Point, Double)
entry = do x <- randomIO
y <- randomIO
t <- randomIO
return ((x,y),t)
Also, should I be worried about having more than one IO monad - it
seems odd encapsulating the "outside world" more than once.
2. Why does the following break finite lists? Wouldn't they just
become lazy lists that evaluate to finite lists once map or length or
whatever is applied?
> Now, if this were changed to
> ~(x:xs) >>= f = f x ++ (xs >>= f)
> (a lazy pattern match) then your listList2 would work, but finite
> lists would stop working.
Thanks again,
Andrew
On Fri, Feb 16, 2001 at 05:51:08PM -0500, Scott Turner wrote:
[...]
> listList1 works because it has a different meaning from listList2
> and listList3. Note that the length of listList1 is explicitly 1,
> but the length of the result list in listList2 and listList3 cannot
> be determined.
>
> Thus, the following "works" because the length of listList5 is
> expressed apart from evaluating listList5.
> listList5 = do rest <- [head listList5]
> return (1:rest)
>
>
> >From another point of view, the (1:rest) _is_ a lazy construct.
> However, rest <- listList2 demands to know, at a minimum,
> what's the length of listList2. See the definition of the list type
> as a monad in Prelude.hs,
> (x:xs) >>= f = f x ++ (xs >>= f)
> [] >>= f = []
>
> Now, if this were changed to
> ~(x:xs) >>= f = f x ++ (xs >>= f)
> (a lazy pattern match) then your listList2 would work, but finite
> lists would stop working.
> -- Scott
>
> >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))
--
http://www.andrewcooke.free-online.co.uk/index.html