Hi everyone,

I have written the following program to find "magic numbers" i.e. integers 'n' such 
that both (n+1) and (n/2+1) are perfect squares.

-- Program to find magic numbers
Import IO

main :: IO ()
main =
        do
        print (filter magicP sqs)

sqs :: [Int]
sqs = [x*x | x <- [1,3..mAX]]

magicP :: Int -> Bool
magicP x
        | ((x-1) `mod` /= 0 = False
        | otherwise         = (((x-1) `div` 2) + 1) `elem` sqs)

mAX :: Int
mAX = 20000

-- End of listing

If I try to run the program (compiled using GHC 6), it calculates all members 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 the monadic IO in Haskell? I am a Haskell 
newbie and can't even claim having 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

_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to