我想在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
我该如何解决这个问题?
答案 0 :(得分:0)
您可以使用大括号和分号代替空格规则:
Prelude> let {charName 'a' = "Albert"; charName 'b' = "Broseph"; charName 'c' = "Cecil"}
Prelude> charName 'a'
"Albert"
Prelude> charName 'b'
"Broseph"