I'm trying to grok Haskell's laziness. In this example, why does the seq not force youTyped to wait for s to be fully evaluated, before returning a result to putStrLn?--------------------import System.IO (hSetBuffering,stdin,BufferMode(..))youTyped :: String -> StringyouTyped s = s `seq` "you typed: " ++ smain = dohSetBuffering stdin NoBufferings <- getContentslet l = map youTyped $ lines $ takeWhile ( /= '\04' ) smapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)--------------------The output looks like:--------------------$ runhaskell seqLazy.hs
Start Typing (Ctrl-D to exit):
Hyou typed: Heelllloo wwoorrlldd
fyou typed: faaiill
^D
--------------------Changing seq, to deepseq does the trick though.--------------------import System.IO (hSetBuffering,stdin,BufferMode(..))import Control.DeepSeqyouTyped :: String -> StringyouTyped s = s `deepseq` "you typed: " ++ smain = dohSetBuffering stdin NoBufferings <- getContentslet l = map youTyped $ lines $ takeWhile ( /= '\04' ) smapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)--------------------Output:
$ runhaskell deepSeqLazy.hs
Start Typing (Ctrl-D to exit):
Hello world
you typed: Hello world
success
you typed: success
^D
When does it make sense to use seq then?
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners