fromEnum (c :: Char)

Don Stewart dons at galois.com
Wed Sep 9 08:10:43 EDT 2009


mechvel:
> People,
> I need to convert  Char <-> Int  in a possibly _standard_ way for 
> Haskell -- and also in an efficient way. In particular, it must not 
> spend 100 comparisons in a look through the listing of Char.
> 
> I use               ord :: Char -> Int  and  chr :: Int -> Char.

ord and chr are correct, yes.

    ord :: Char -> Int
    ord (C# c#) = I# (ord# c#)

    chr :: Int -> Char
    chr (I# i#) | int2Word# i# `leWord#` int2Word# 0x10FFFF# = C# (chr# i#)
                | otherwise                                  = error "Prelude.chr: bad argument"

    unsafeChr :: Int -> Char
    unsafeChr (I# i#) = C# (chr# i#)

These are usually resolved at compile time as casts.
    
-- Don


More information about the Glasgow-haskell-users mailing list