如何在Haskell中获取字符的ASCII值?
问题描述:
如何获取Haskell中字符的ASCII值?根据我阅读的内容
How to get the ASCII value of a character in Haskell? I've tried to use the ord
function in GHCi, based on what I read here bug the the error message:
不在范围内:"ord"
Not in scope: `ord'
例如:
GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Prelude> ord 'a'
<interactive>:1:0: Not in scope: `ord'
Prelude>
我在做什么错了?
答
如 Travis Brown 所述,则必须从模块Data.Char
:
As Travis Brown indicated in a comment, you have to import the ord
function from the module Data.Char
:
import Data.Char (ord)
main = print (ord 'a')
仅 Prelude
模块是默认加载的,所有其他模块都必须显式导入.
Only the Prelude
module is loaded by default, all other modules have to be imported explicitly.