[Haskell-beginners] unwanted Fractional constraint

David McBride toad3k at gmail.com
Fri Dec 11 13:42:03 UTC 2020

The problem is that the moment you divided by two, it can no longer be an
instance of Num.

Instances of Num include: Integers, Ints and Words.  Once divided, you can
no longer use that result in places where an Integer is required, because
it won't be.

What you can do is use the `div` function which will round down to the
nearest Integer value.  Then it is an instance of Integral, which includes
Integers, Ints, and Words (but floating point types)

You can also use `round`, `floor`, or `ceiling` to round your result to an
appropriate integer after you've divided.

On Fri, Dec 11, 2020 at 8:04 AM M Douglas McIlroy <
m.douglas.mcilroy at dartmouth.edu> wrote:

> For rational functions that take on integer values at integer
> arguments, for example n*(n+1)/2, is there a way to doctor the
> corresponding Haskell definition
>     f n = n*(n+1)/2
> so that the type signature becomes
>     f :: Num a => a -> a
> rather than
>     f :: Fractional a => a -> a
> Doug McIlroy
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20201211/5b9cbb0b/attachment.html>

More information about the Beginners mailing list