开发者

Forced strictness for lists in haskell

开发者 https://www.devze.com 2023-02-22 12:55 出处:网络
I made really time consum开发者_运维问答ing 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 understa

I made really time consum开发者_运维问答ing 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?


(!) 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消