Forced strictness for lists in haskell

喜你入骨 提交于 2019-12-10 01:32:12

问题


I made really time consuming algorithm which produces a short string as the result. When I try to print it (via putStrLn) it appears on the screen character by character. I did understand why that happened, and I tried to force evaluation of the string before actual printing.

myPrint !str = putStrLn str

But this help very little. When I ran the program in debug I noticed that the !str forced evaluation only for the first character.

Does anyone know why is that, and how to deal with this?


回答1:


(!) translates into seq, which evaluates strictly to Weak Head Normal Form -- that is, it only evaluates to the outermost constructor. To evaluate more deeply, you need a "deep" form of seq.

This is known as deepseq.

It is in the deepseq package.




回答2:


seqList :: [a] -> ()
seqList [] = ()
seqList (x:xs) = strictList xs


来源:https://stackoverflow.com/questions/5558043/forced-strictness-for-lists-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!