2

I want to do the following in ghci, but apparently it does not allow me to do so:

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

I could have done:

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

But still, because of no charName :: Char -> String, it would fail the following:

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

How can I solve this issue?

4

1 回答 1

0

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

Prelude> let {charName 'a' = "Albert"; charName 'b' = "Broseph"; charName 'c' = "Cecil"}
Prelude> charName 'a'
"Albert"
Prelude> charName 'b'
"Broseph"
于 2013-07-23T05:54:50.673 回答