我在Haskell教程中偶然发现了一些似乎对我不起作用的代码。
fromEnum :: Char -> Int
toEnum :: Int -> Char
offset :: Int
offset = fromEnum 'A' - fromEnum 'a'
toUpper :: Char -> Char
toUpper ch = toEnum (fromEnum ch + offset)
本摘录的教科书指出以下转换函数将小写字母Char转换为大写字母。但是,当我尝试运行脚本时,我收到以下错误消息:
eval.hs:1:1:
The type signature for ‘fromEnum’ lacks an accompanying binding
(The type signature must be given where ‘fromEnum’ is declared)
eval.hs:2:1:
The type signature for ‘toEnum’ lacks an accompanying binding
(The type signature must be given where ‘toEnum’ is declared)
Failed, modules loaded: none.
我是Haskell的新手,有人能告诉我这里缺少什么吗?教科书错了吗?
答案 0 :(得分:4)
删除以下行:
fromEnum :: Char -> Int
toEnum :: Int -> Char
这些功能已由标准库定义。通过添加上述行,您将尝试重新定义它们,从而触发错误。
本教程可能提到它们存在,并且不会邀请您在程序中添加这些功能。