[Haskell-cafe] Rewriting Char.ord

Stefan O'Rear stefanor at cox.net
Sat Sep 29 10:26:57 EDT 2007


On Sat, Sep 29, 2007 at 03:11:20PM +0100, PR Stanley wrote:
> Hi
> ord :: Char -> Int
> ord c = sum [1 | x <- ['\0'..'\255'], x < c]
>
> Any comments? Any alternatives?
> Cheers, Paul

It's waay slow, it breaks for characters >255.

ord :: Char -> Int
ord = fromEnum

As Char is an instance of 'Enum'.

If you think this is cheating, then consider that (<) for Char is
probably implemented using ord on some level.  You need primitives
somewhere...

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070929/dbdf4c5b/attachment-0001.bin


More information about the Haskell-Cafe mailing list