Hello,
My question was why ghc does not evaluate lazy in the following
example.
------------------------------------------------------------------
data Maybe a = Nothing | Just a
g i j = mod (i+j) 5 :: Int
-- just to do something non-trivial with i,j
f [] = Just [] :: Maybe [Int]
f (j:js) =
case f (map (g j) js) of Just js' -> Just (j:js')
_ -> Nothing
main = let { n = 1000 :: Int; js = [1..n]; k = 8 :: Int }
in
case h js of Just xs -> print (show (xs!!k) )
_ -> error "error"
------------------------------------------------------------------
In ghc (time for k = 8) = (time for k = 800)
which cannot be so for a lazy evaluation.
==================================================================
==================================================================
I thank Hans Loidl and Alastair Reid for the notices.
A.Reid writes
> It is impossible to get part of the answer without checking that the
> entire answer will be available (ie no errors occur anywhere) - which
> requires that you calculate the entire answer. This is a feature of
> this style of error handling --- it makes your program much stricter.
Yes, I see now.
And not only `error', also (Just x) | Nothing has to be solved.
Now the crucial experiment might be to set [1..]
instead of [1..1000]
in `main'.
The result for k = 1 will be an infinite cycle.
Further, if we replace everywhere
case f ... of Just js' -> ...
_ -> ...
with let (Just js') = f ... in ...
it improves and returns a number.
Thank you.
Sergey Mechveliani [EMAIL PROTECTED]