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

Chaddaï Fouché chaddai.fouche at gmail.com
Mon Sep 9 19:59:28 CEST 2013


On Mon, Sep 9, 2013 at 7:26 PM, Bryan Vicknair <bryanvick at gmail.com> wrote:

> 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?
>
>
It would be a serious bug if that was true since lazyness shouldn't change
the semantic of a program, except sometimes by allowing the program to
terminate where the strict version wouldn't.

On the other hand I can't reproduce your bug, couldn't you provide more
details (including GHC version and parsing code)

-- 
Jedaï
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130909/5f661079/attachment.htm>


More information about the Beginners mailing list