
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

On Mon, Jul 21, 2003 at 05:41:32PM +0530, AJ wrote:
Hi everyone,
Hi!
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.
Import IO
main :: IO () main = do print (filter magicP sqs)
Now my question is - How do I reproduce such "lazy printing" behaviour in GHC?
My guess would be to try running main = putStr $ unlines $ map show $ filter magicP sqs It may be that print is doing something like checking the length of the list which would of course not allow for lazy printing. -- David Roundy http://www.abridgegame.org

On Monday, 2003-07-21, 14:36, CEST, David Roundy wrote:
[...]
My guess would be to try running
main = putStr $ unlines $ map show $ filter magicP sqs
It may be that print is doing something like checking the length of the list which would of course not allow for lazy printing.
Is print allowed to do so? Maybe, the problem has something to do with buffering. I encountered similar problems with GHCi. Wolfgang

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.
My guess would be that you're seeing buffering behaviour where GHC fills up a buffer of characters for output and flushes it every 1000, 4000 or whatever characters. IIRC correctly, buffering is off by default in Hugs but on by default in GHC. You can turn this off with the Haskell 98 function hSetBuffering described in section 11.4.2 of the library report. -- Alastair

On Monday 21 Jul 2003 7:21 pm, Alastair Reid wrote:
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.
My guess would be that you're seeing buffering behaviour where GHC fills up a buffer of characters for output and flushes it every 1000, 4000 or whatever characters.
IIRC correctly, buffering is off by default in Hugs but on by default in GHC.
You can turn this off with the Haskell 98 function hSetBuffering described in section 11.4.2 of the library report.
Thanks for the pointer! Ok so I added the "hSetBuffering stdout NoBuffering" line to my main function and still got the same 'nonlazy' behaviour, or so I thought. It took me some time to figure out what was going on. I am using emacs to write my code and I was calling a.out from inside it. Obviously it was emacs that was holding back the output until the program terminated! Now I feel kinda stupid :-( Anyway since I am also lazy enough to dislike switching back to the shell whenever I want to test my programs, here is another question - how do I make emacs give me the output AS AND WHEN it recieves it? If you are planning to say "Go read the manual" please also add some pointers to which part of the manual 'cos by the size of it, I'll be looking for *some* time. :-) - AJ

AJ wrote:
Ok so I added the "hSetBuffering stdout NoBuffering" line to my main function and still got the same 'nonlazy' behaviour, or so I thought. It took me some time to figure out what was going on. I am using emacs to write my code and I was calling a.out from inside it. Obviously it was emacs that was holding back the output until the program terminated! Now I feel kinda stupid :-(
Anyway since I am also lazy enough to dislike switching back to the shell whenever I want to test my programs, here is another question - how do I make emacs give me the output AS AND WHEN it recieves it?
If you put an ampersand ("&") after the command, Emacs will run it in
the background.
--
Glynn Clements

On Friday 25 Jul 2003 6:52 pm, you wrote:
AJ wrote:
Ok so I added the "hSetBuffering stdout NoBuffering" line to my main function and still got the same 'nonlazy' behaviour, or so I thought. It took me some time to figure out what was going on. I am using emacs to write my code and I was calling a.out from inside it. Obviously it was emacs that was holding back the output until the program terminated! Now I feel kinda stupid :-(
Anyway since I am also lazy enough to dislike switching back to the shell whenever I want to test my programs, here is another question - how do I make emacs give me the output AS AND WHEN it recieves it?
If you put an ampersand ("&") after the command, Emacs will run it in the background.
Ok, thanks for all the help! - AJ
participants (5)
-
AJ
-
Alastair Reid
-
David Roundy
-
Glynn Clements
-
Wolfgang Jeltsch