Haskell: Int to Char

I use a function fromEnumto convert a character to the corresponding ASCII Int. For example:

fromEnum 'A'

returns 65.

Now, if I have a function that did:

(fromEnum 'A')+1

And then I wanted to convert the return value (66) to Char, which would be "B". What is the best way to do this?

Thanks!

+4
source share
2 answers

You can use succto implement the desired behavior:

nextLetter :: Char -> Char
nextLetter c
    | c == 'Z' = 'A'
    | otherwise = succ c
+4
source

You can use the functions of ord :: Char -> Intand chr :: Int -> Charfrom Data.Char.

> chr (ord 'a' + 1)
'b'

But do not forget import Data.Charin the source file or :m +Data.Charin ghci.

Same with fromEnum :: Enum a => a -> Intand toEnum :: Enum a => Int -> a:

toEnum (fromEnum 'a' + 1) :: Char

haskell, , . :: Char:

isLower $ toEnum  (fromEnum 'a' + 1)

isLower Char -> Bool. , toEnum (fromEnum 'a' + 1) Char.

, bheklilr :) .

+5

All Articles