<div dir="ltr"><div style="font-size:13px">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?</div><div style="font-size:13px"><br></div><div style="font-size:13px">--------------------</div><div style="font-size:13px">import System.IO (hSetBuffering,stdin,BufferMode(..))</div><div style="font-size:13px"><br></div><div style="font-size:13px">youTyped :: String -> String</div><div style="font-size:13px">youTyped s = s `seq` "you typed: " ++ s</div><div style="font-size:13px"><br></div><div style="font-size:13px">main = do</div><div style="font-size:13px"><span style="white-space:pre-wrap">   </span>hSetBuffering stdin NoBuffering</div><div style="font-size:13px"><span style="white-space:pre-wrap"> </span>s <- getContents</div><div style="font-size:13px"><span style="white-space:pre-wrap">     </span>let l = map youTyped $ lines $ takeWhile ( /= '\04' ) s</div><div style="font-size:13px"><span style="white-space:pre-wrap"> </span>mapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)</div><div style="font-size:13px">--------------------</div><div style="font-size:13px"><br></div><div style="font-size:13px">The output looks like:</div><div style="font-size:13px"><br></div><div style="font-size:13px"><div>--------------------</div><p style="margin:0px;font-size:11px;font-family:Menlo">$ runhaskell seqLazy.hs </p><p style="margin:0px;font-size:11px;font-family:Menlo">Start Typing (Ctrl-D to exit):</p><p style="margin:0px;font-size:11px;font-family:Menlo">Hyou typed: Heelllloo  wwoorrlldd</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">fyou typed: faaiill</p><p style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">^D</p></div><div style="font-size:13px">--------------------</div><div style="font-size:13px"><br></div><div style="font-size:13px">Changing seq, to deepseq does the trick though.</div><div style="font-size:13px"><br></div><div style="font-size:13px"><div>--------------------</div><div>import System.IO (hSetBuffering,stdin,BufferMode(..))</div><div>import Control.DeepSeq</div><div><br></div><div>youTyped :: String -> String</div><div>youTyped s = s `deepseq` "you typed: " ++ s</div><div><br></div><div>main = do</div><div><span style="white-space:pre-wrap">       </span>hSetBuffering stdin NoBuffering</div><div><span style="white-space:pre-wrap">  </span>s <- getContents</div><div><span style="white-space:pre-wrap">      </span>let l = map youTyped $ lines $ takeWhile ( /= '\04' ) s</div><div><span style="white-space:pre-wrap">  </span>mapM_ putStrLn ("Start Typing (Ctrl-D to exit):":l)</div></div><div style="font-size:13px">--------------------</div><div style="font-size:13px"><br></div><div style="font-size:13px">Output:</div><div style="font-size:13px"><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">$ runhaskell deepSeqLazy.hs </p><p style="margin:0px;font-size:11px;font-family:Menlo">Start Typing (Ctrl-D to exit):</p><p style="margin:0px;font-size:11px;font-family:Menlo">Hello world</p><p style="margin:0px;font-size:11px;font-family:Menlo">you typed: Hello world</p><p style="margin:0px;font-size:11px;font-family:Menlo">success</p><p style="margin:0px;font-size:11px;font-family:Menlo">you typed: success</p><p style="margin:0px;font-size:11px;font-family:Menlo">^D</p><p style="margin:0px;font-size:11px;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;font-family:Menlo">When does it make sense to use seq then?</p></div></div>