[Haskell-beginners] truncate results depend on strict/lazy

Bryan Vicknair bryanvick at gmail.com
Mon Sep 9 19:26:40 CEST 2013


Deep in a WAI web app, I have a function that converts a String from a web form
like "0.12" to an Int 12.  Converting "0.12" to the Float 0.12 is working.
However, converting the Float 0.12 to the Int 12 does not work as expected
unless I use the trace function.

In the following, f = 0.12::Float, gotten from a function that parses "0.12"
into 0.12.

In the following expression, the result is: Success (Just 11).

> Success $ Just $ truncate (f * 100)

In the following expression, the result is: Success (Just 12)

> let expanded =  f * 100
>     ans      = truncate expanded
> in trace (show expanded) $ Success $ Just $ ans

That made me think that "f * 100" had to be strictly evaluated before given to
truncate for some reason, so I tried using seq to get the same effect, but that
didn't work.  Am I correct in assuming that laziness has something to do with
this problem?




More information about the Beginners mailing list