12

I am using the function fromEnum to convert a character to its corresponding ASCII Int. For example:

fromEnum 'A'

returns 65.

Now, assuming I had a function that did:

(fromEnum 'A')+1

And then wanted to convert the returned value (66) to a Char which would be 'B'. What is the best way of doing so?

Thanks!

3
  • 3
    Why not use succ? (hoogle) Commented Dec 18, 2013 at 13:44
  • @MattFenwick that is because I want to set it so that if the char becomes 'Z' the successive one is 'A', but I guess I can work around that with your suggestion, thanks! :) Commented Dec 18, 2013 at 13:47
  • 3
    I'd suggest rewriting the question to describe the actual problem you're trying to solve, since your comment seems to imply that the problem you've described isn't the root one. Commented Dec 18, 2013 at 13:50

3 Answers 3

10

You can use ord :: Char -> Int and chr :: Int -> Char functions from Data.Char.

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

But don't forget to import Data.Char in source file or :m +Data.Char in ghci.

The same thing with fromEnum :: Enum a => a -> Int and toEnum :: Enum a => Int -> a:

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

The last part of this expression says haskell what type we are expecting to help the type system infer right type. But in this case we can drop :: Char:

isLower $ toEnum  (fromEnum 'a' + 1)

because isLower has type Char -> Bool. So it is expecting toEnum (fromEnum 'a' + 1) to be Char.

Anyway, the solution of bheklilr is good enough :) I just wanted to show you few ways of solving your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

The idiomatic way for the more general problem ord and chr. I think it was a good idea to point that out. However, for this particular problem, @bheklilrs solution was clearly shorter.
5

You can use succ to implement the behavior you want:

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

Comments

0
-- Here is my code ex: intToChar 123 // "123"
intToChar :: Integer -> String
intToChar x 
    | x == 0 = ['0']
    | x == 1 = ['1']
    | x == 2 = ['2']
    | x == 3 = ['3']
    | x == 4 = ['4']
    | x == 5 = ['5']
    | x == 6 = ['6']
    | x == 7 = ['7']
    | x == 8 = ['8']
    | x == 9 = ['9']
    | x > 9 = (intToChar (x `div` 10)  ++ intToChar (mod x 10 ) )

3 Comments

Welcome to StackOverflow. Unfortunately your contribution is not useful here: it solves a different problem from the one asked about in the question. (Also, this isn't good code. You should avoid all the duplication. Your function can be written as intToString x | x<10 = [toEnum (x + fromEnum '0')] | otherwise = ....)
Thank you for your excellent reply. I hope someone may need it, though It may not good one.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.