在haskell交互式shell中定义函数

时间:2013-07-23 05:33:19

标签: haskell

我想在ghci中执行以下操作,但显然它不允许我这样做:

charName :: Char -> String  
charName 'a' = "Albert"  
charName 'b' = "Broseph"  
charName 'c' = "Cecil" 

我本可以做到:

let charName 'a' = "Albert"  
let charName 'b' = "Broseph"  
let charName 'c' = "Cecil" 

但是,由于没有charName :: Char -> String,它将失败以下:

charName 'a' 
"*** Exception: <interactive>:38:5-26: Non-exhaustive patterns in function charName

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用大括号和分号代替空格规则:

Prelude> let {charName 'a' = "Albert"; charName 'b' = "Broseph"; charName 'c' = "Cecil"}
Prelude> charName 'a'
"Albert"
Prelude> charName 'b'
"Broseph"