Laziness
Jon Fairbairn
Jon.Fairbairn@cl.cam.ac.uk
Sat, 02 Aug 2003 14:47:46 +0100
On 2003-08-02 at 14:36PDT "Dominic Steinitz" wrote:
> Could someone explain to me why this doesn't work
>
> test l =
> hs
> where
> hs = map (\x -> [x]) [0..abs(l `div` hLen)]
> hLen = length $ head hs
>
> whereas this does
>
> test l =
> hs
> where
> hs = map (\x -> [x]) (0:[1..abs(l `div` hLen)])
> hLen = length $ head hs
>
> I would have thought laziness would allow the compiler to
> know that hs would contain at least one element and
> therefore calculate hLen.
Laziness isn't enough to tell it that. It would also have to
know that abs never returns an answer less than zero
([0 .. -1] == []).
All the compiler knows is that abs returns an integer.
Jón
PS I don't know the general policy, but I for one dislike
getting emails in HTML unless it's /absolutely/ necessary for
the content. Multipart/alternative doesn't help much either.
--
Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk