Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the next letter in the alphabet of given letter wrapped around in Haskell [closed]

I'm trying to implement a function that returns the next letter in alphabetical order. For example:

> returnNext 'A'
  'B'

But also:

> returnNext 'Z'
  'A'

The function should thus cycle between char codes in alphabetical order (mod 26).

like image 413
user3094936 Avatar asked Oct 28 '25 20:10

user3094936


1 Answers

Two ways come to mind

import Data.Char
returnNext c = chr (((1 + ord c - ord 'A') `mod` 26) + ord 'A')

Which is kind of ugly to say the least.

And:

returnNext 'Z' = 'A'
returnNext c = chr (ord c + 1)

Both behave differently when not given a letter of the alphabet, but since you didn't specify what should happen in this case I'll assume it's OK.

like image 102
Guido Avatar answered Oct 30 '25 23:10

Guido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!