Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. unwanted Fractional constraint (M Douglas McIlroy) 2. Re: unwanted Fractional constraint (David McBride) 3. Re: unwanted Fractional constraint (David James) ---------------------------------------------------------------------- Message: 1 Date: Fri, 11 Dec 2020 08:03:03 -0500 From: M Douglas McIlroy <m.douglas.mcil...@dartmouth.edu> To: beginners@haskell.org Subject: [Haskell-beginners] unwanted Fractional constraint Message-ID: <cakh6pivhxddtx4exanfjk23fqm2geocoix-ymwg_kymf8nq...@mail.gmail.com> Content-Type: text/plain; charset="UTF-8" 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 ------------------------------ Message: 2 Date: Fri, 11 Dec 2020 08:42:03 -0500 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] unwanted Fractional constraint Message-ID: <CAN+Tr42FueMGDqwg+ueZ9emk-kUU7PC_MnqFA-kFB3OARYz=v...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 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.mcil...@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@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-0001.html> ------------------------------ Message: 3 Date: Fri, 11 Dec 2020 16:38:46 +0000 From: David James <dj112...@outlook.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] unwanted Fractional constraint Message-ID: <am6pr04mb4214c1730b0fcf72c079bf16b6...@am6pr04mb4214.eurprd04.prod.outlook.com> Content-Type: text/plain; charset="windows-1252" [Attempting to resend] Hi - I think your logic is: I can define these two: fFrac :: Fractional a => a -> a fFrac n = n * (n+1) / 2 fInt :: Integral a => a -> a fInt n = n * (n+1) `div` 2 so that, e.g. fFrac (5.0 :: Float) == 15.0 :: Float fInt (5 :: Integer) == 15 :: Integer And all number types are either Integral or Fractional, so surely I should be able to define a single function of type: f :: Num a => a -> a This would seem reasonable, but I think there’s a problem with the last assumption. It is indeed possible for other types to be instances of Num, but not of Integral or Fractional. For example, I could define: instance Num Bool where fromInteger 0 = False fromInteger _ = True (+) = (&&) (*) = (||) abs = id signum _ = True negate = not Now this would probably be pretty dumb (and probably doesn’t comply with expectations<https://hackage.haskell.org/package/base-4.14.0.0/docs/Prelude.html#t:Num>), but is possible. (And also pretty dumb to define it without also define an instance Integral Bool where ..., but still possible). So I don’t think f :: Num a => a -> a could be possible, since Num by itself (& the dumb Bool instance) has no way to do the division. (At least that I can think of, but would be very interested to hear if there is). Regards, David. From: Beginners <beginners-boun...@haskell.org> on behalf of M Douglas McIlroy <m.douglas.mcil...@dartmouth.edu> Sent: Friday, December 11, 2020 1:03:03 PM To: beginners@haskell.org <beginners@haskell.org> Subject: [Haskell-beginners] unwanted Fractional constraint 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@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/139c3d1a/attachment-0001.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: F5B325D295EC4EFD853932C4105331AE.png Type: image/png Size: 144 bytes Desc: F5B325D295EC4EFD853932C4105331AE.png URL: <http://mail.haskell.org/pipermail/beginners/attachments/20201211/139c3d1a/attachment-0001.png> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 149, Issue 5 *****************************************