如何返回在Haskell包裹的给定信件的字母表中的下一个字母 [英] How to return the next letter in the alphabet of given letter wrapped around in Haskell

查看:278
本文介绍了如何返回在Haskell包裹的给定信件的字母表中的下一个字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个函数,按字母顺序返回下一个字母。例如:

 > returnNext'A'
'B'

但也:

 > returnNext'Z'
'A'

函数应该按字母顺序



<$>

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

这至少是一种丑陋。



和:

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

当没有给出字母表的字母时,两者的行为不同,但由于您没有指定应该发生什么这种情况下,我会假设它没关系。


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).

解决方案

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.

这篇关于如何返回在Haskell包裹的给定信件的字母表中的下一个字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆