2010-11-22 55 views
12

我是哈斯克爾的新手,試圖完成凱撒密碼演習。如何使用Haskell庫函數?

.hs文件中,我定義了以下功能:

let2int :: Char -> Int 
let2int c = ord c - ord 'a' 

然後我試圖通過鍵入:l caeser.hs加載到這個GHCI,我得到了以下錯誤消息:

[1 of 1] Compiling Main    (caeser.hs, interpreted) 
caeser.hs:2:12: Not in scope: `ord' 
caeser.hs:2:20: Not in scope: `ord' 

從我使用的書,我的印象是,ordchr是在字符和整數之間轉換的標準函數,但似乎很明顯我需要「導入」它們或其他東西。這是如何完成的?

+2

順便說一句,你拼錯「凱撒」 – wnoise 2010-11-22 19:48:32

+0

我一直在尋找加載從GHCI命令行庫,我發現你可以使用這個命令在命令行: `:m Data.Char` – metatron 2018-02-03 11:21:46

回答

3

如果您使用hoogle搜索ord,您會看到該函數存在於/由Data.Char模塊導出。因此,只需導入此模塊:

import Data.Char 

學習使用hoogle。許多SO Haskell的問題都是人們不瞭解Hoogle的結果......有時他們也不知道谷歌(不要阻止你詢問,而是使用hoogle)。

今後,對於可能有衝突的名稱與現有的功能,較大的圖書館,你可以限制你的導入只是你所關心的功能:

import Data.Char (ord) 

或者導入qualified

import qualified Data.Char as C 
... 
func x y = C.ord x - C.ord y 

(第三種方法,使用hiding,工作但我討厭那種方法)

9

在「Haskell 2010」,ord個生活Data.Char

所以你要import Data.Charimport Data.Char (ord)

在 「哈斯克爾98」,ord可以在模塊Char中找到。

查找功能及其模塊的一個偉大的工具是

http://www.haskell.org/hoogle/