
Thanks for the help. I now have another question related to this.
When I write:
isInt :: Double -> Bool
isInt x = x == fromInteger (floor x)
niceShow :: Double -> String
niceShow x = if isInt x then show (floor x :: Int) else show x
I get a warning about a "too strict if". If I then follow the recommendation
and change niceShow to be
show (if isInt x then (floor x :: Int) else x)
Then I get an error that Couldn't match expected type `Int' with actual type
`Double' which makes sense because floor x :: Int produces an Int but x
alone is a Double. Surely hlint could have figured this out from the type
signatures and not made the recommendation to change my if structure?
What's going on here? And what best to do? What is a "too strict if" anyway?
a
From: Beginners [mailto:beginners-bounces@haskell.org] On Behalf Of David
McBride
Sent: 30 September 2013 21:54
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell
Subject: Re: [Haskell-beginners] Defaulting the following constraint ....
It is because you do a floor on x, returning a where a is an Integral, but
which Integral is it? Is it an Int or an Integer? Well it decides to
default it to integer because that is what ghci does as it is the safe
option, but it decided to warn you about it just so you are aware. Afterall
integers are slower than ints, and you might have wanted an int. You can
silence the warning by telling it what to do:
niceShow x = if (isInt x) then show (floor x :: Int) else show x
niceShow x = if (isInt x) then show (floor x :: Integer) else show x
On Mon, Sep 30, 2013 at 4:06 PM, Alan Buxton