[Haskell-cafe] Perl-style numeric type

Brent Yorgey byorgey at gmail.com
Wed Jun 20 10:43:14 EDT 2007


On 6/20/07, Henning Thielemann <lemming at henning-thielemann.de> wrote:
>
>
> How about
> 1 % floor pi
>
> ?
>
> Already two examples for the Wiki which I used to start the Wiki article:
>   http://www.haskell.org/haskellwiki/Generic_numeric_type
>

What about the function isSquare?

isSquare :: (Integral a) => a -> Bool
isSquare n = (floor . sqrt $ fromIntegral n) ^ 2 == n

Is there any way to write that without the fromIntegral?  If you leave out
the fromIntegral and the explicit type signature, it type checks, but the
type constraints are such that there are no actual types that you can call
it on.

As I think about it more, I guess one of my biggest goals is essentially to
have an integral type which can silently convert to a rational or floating
type when necessary (e.g. you should be able to call sqrt on an integral
type and have it implicitly convert to floating).  Perhaps this actually has
less to do with scripting-language-style numeric types than it does with
languages (e.g. Java) that do implicit type conversions in directions where
no information is lost -- e.g. you can take the sqrt of an int and get a
double, but if you want to change a double into an int you have to
explicitly truncate or round or whatever.

-Brent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070620/68c099c2/attachment.htm


More information about the Haskell-Cafe mailing list