An unsigned int class.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

An unsigned int class.

derek riemer
Is there anything in haskell that represents an unsigned Int class?
namely, something bounded below at 0? If not, is it possible to define
an Int in this way to add the bounded constraint (I can't immagine
trying to define Int like this, there has got to be a better way that I
haven't quite understood in my study of haskell).
data UnsignedInt = 0 | 1 | 2 | (...) for a long time doing that ...
deriving (Show, Eq, Ord, Bounded, Read)
Thanks,
Derek
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: An unsigned int class.

Brandon Allbery
On Fri, Jul 31, 2015 at 11:30 AM, derek riemer <[hidden email]> wrote:
Is there anything in haskell that represents an unsigned Int class?

It's a type, not a class. "Class" implies something very different in Haskell.

And I think you're looking for the "Word" type from Data.Word.
http://lambda.haskell.org/platform/doc/current/ghc-doc/libraries/haskell2010-1.1.1.0/Data-Word.html

--
brandon s allbery kf8nh                               sine nomine associates
[hidden email]                                  [hidden email]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: An unsigned int class.

Marcin Mrotek
Hello,

If you're interested in numbers from 0 upto bignums, there's also Numeric.Natural: https://hackage.haskell.org/package/base-4.8.1.0/docs/Numeric-Natural.html

Best regards,
Marcin Mrotek

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: An unsigned int class.

David Obwaller
In reply to this post by derek riemer
Hi,

On Fri, Jul 31, 2015 at 09:30:35AM -0600, derek riemer wrote:
> Is there anything in haskell that represents an unsigned Int class?
> namely, something bounded below at 0?

The module Data.Word provides unsigned integral types with
modular arithmetic: Word (same size as Int), Word8, Word16,
etc.

    $ ghci
    > import Data.Word
    ...
    > maxBound :: Word
    18446744073709551615
    > maxBound :: Word8
    255
    > maxBound :: Word32
    4294967295
    > 0 - 1 :: Word8
    255
    > maxBound + 1 :: Word8
    0

Is that what you are looking for?

http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Word.html

David
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners