lazy Printing question
AJ
skywalker@gamebox.net
Mon, 21 Jul 2003 17:41:32 +0530
Hi everyone,
I have written the following program to find "magic numbers" i.e. integer=
s 'n' such that both (n+1) and (n/2+1) are perfect squares.
-- Program to find magic numbers
Import IO
main :: IO ()
main =3D
=09do
=09print (filter magicP sqs)
sqs :: [Int]
sqs =3D [x*x | x <- [1,3..mAX]]
magicP :: Int -> Bool
magicP x
=09| ((x-1) `mod` /=3D 0 =3D False
=09| otherwise =3D (((x-1) `div` 2) + 1) `elem` sqs)
mAX :: Int
mAX =3D 20000
-- End of listing
If I try to run the program (compiled using GHC 6), it calculates all mem=
bers of the list and then prints the whole list in the end. Since Haskell=
is 'lazy' I was expecting behaviour similar to HUGS where it prints the =
numbers as it finds them. Does this behaviour have something to do with t=
he monadic IO in Haskell? I am a Haskell newbie and can't even claim havi=
ng understood the "gentle introduction" properly.
Now my question is - How do I reproduce such "lazy printing" behaviour in=
GHC?
Wait... I thought of another one :-), how do I speed up this program?
- AJ