Haskell 关于“的错误”;“无约束力的类型签名”;

Haskell 关于“的错误”;“无约束力的类型签名”;,haskell,Haskell,我在Haskell中遇到ASCII问题 fromEnum :: Char -> Int toEnum :: Int -> Char offset :: Int offset = fromEnum 'A' - fromEnum 'a' toUpper :: Char -> Char toUpper ch = toEnum (fromEnum ch + offset) 此脚本给出一个错误: The type signature 'fromEnum' lacks an acco

我在Haskell中遇到ASCII问题

fromEnum :: Char -> Int
toEnum :: Int -> Char

offset :: Int
offset = fromEnum 'A' - fromEnum 'a'

toUpper :: Char -> Char
toUpper ch = toEnum (fromEnum ch + offset)
此脚本给出一个错误:

The type signature 'fromEnum' lacks an accompanying binding
The type signature must be given where 'fromEnum is declared

The type signature 'toEnum' lacks an accompanying binding
The type signature must be given where 'toEnum is declared

fromnum
toEnum
是标准函数

请试试这个:

offset :: Int
offset = fromEnum 'A' - fromEnum 'a'

toUpper :: Char -> Char
toUpper ch = toEnum (fromEnum ch + offset)

除非您将此作为练习,否则您应该真正使用模块
Data.Char
中的
toUpper

要做到这一点,需要检查Unicode中大写字母的定义方式——我宁愿依赖标准库:)


另外,请注意,因为您的
toUpper
不是幂等的:例如,您可能会认为
toUpper'A'='A'

您不能为未定义自己的函数编写类型签名。(Enum的
toEnum
都来自
Prelude
模块。)旁注:Haskell
Char
s不是ASCII@用户4394101,请检查我的答案是否有用:)