【发布时间】:2012-11-21 20:26:17
【问题描述】:
在 Haskell 中,函数 Data.Char.isAlpha 检查字符是否为字母,Data.Char.isLetter 也是如此。这些功能之间有什么真正的区别,或者它们可以互换吗?
【问题讨论】:
在 Haskell 中,函数 Data.Char.isAlpha 检查字符是否为字母,Data.Char.isLetter 也是如此。这些功能之间有什么真正的区别,或者它们可以互换吗?
【问题讨论】:
看看sources,它们似乎是等价的。
这里是isLetter 的定义,在 4.3.1.0 中定义
-- derived character classifiers
-- | Selects alphabetic Unicode characters (lower-case, upper-case and
-- title-case letters, plus letters of caseless scripts and modifiers letters).
-- This function is equivalent to 'Data.Char.isAlpha'.
isLetter :: Char -> Bool
isLetter c = case generalCategory c of
UppercaseLetter -> True
LowercaseLetter -> True
TitlecaseLetter -> True
ModifierLetter -> True
OtherLetter -> True
_ -> False
还有isAlpha的definition:
-- | Selects alphabetic Unicode characters (lower-case, upper-case and
-- title-case letters, plus letters of caseless scripts and modifiers letters).
-- This function is equivalent to 'Data.Char.isLetter'.
isAlpha :: Char -> Bool
isAlpha c = iswalpha (fromIntegral (ord c)) /= 0
它们似乎有不同的实现,但它们被记录为具有相同的效果。
【讨论】:
现在没有真正的区别。来自the docs:
isAlpha :: Char -> Bool
选择字母 Unicode 字符(小写、大写和标题大小写字母,加上无大小写脚本的字母和修饰符字母)。这个函数相当于Data.Char.isLetter。
【讨论】: