Question

I have to write a function in HASKELL whose name is siguienteLetra that take a variable and this variable is a letter of alphabet and return the next letter. How can i do that?

example: siguienteLetra 'a' and the function return me 'b' or siguienteLetra 'c' and the function return me 'd'

Was it helpful?

Solution

Hint 1

These two functions from Data.Char will be helpful:

ord :: Char -> Int
chr :: Int -> Char

The first one converts a character to its numerical representation (e.g. 'a' → 97, 'b' → 98) while the other does the reverse conversion (97 → 'a', 98 → 'b'). Using these function in a reasonable way will allow you to implement siguienteLetra.

Hint 2

You will need a third function, besides ord and chr, what should it do? This third function is in Prelude, but it might be difficult to guess its name (depending on your background). Fortunately, the function is very simple and you will easily write it yourself.

Hint 3

What should be the signature of siguienteLetra? Look at the signatures of your three functions (ord, chr and the third one) and think of the order in which they should be combined.

OTHER TIPS

Bolo thanks for your help, that is the result.

module Ejercicio1 where

import Data.Char
import Data.Ord()

siguienteLetra :: Char -> Char    -- you should not put pure computations
                                  -- in the IO Monad!
siguienteLetra y = chr z
 where x = ord y
       z = x+1

ejercicio1 :: IO ()
ejercicio1 = do
    y <- getChar
    print $ siguienteLetra y
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top