这个函数的正确类型声明是什么?

时间:2010-07-14 13:09:12

标签: haskell

以下功能无法加载:

charName :: a -> String
charName 'a' = "Alpha"
charName 'b' = "Bravo"
charName 'c' = "Charlie"
charName 'd' = "Delta"
charName 'e' = "Echo"
charName 'f' = "Foxtrot"
charName 'g' = "Golf"
charName 'h' = "Hotel"
charName 'i' = "India"
charName 'j' = "Juliet"
charName 'k' = "Kilo"
charName 'l' = "Lima"
charName 'm' = "mike"
charName 'n' = "November"
charName 'o' = "Oscar"
charName 'p' = "Papa"
charName 'q' = "Quebec"
charName 'r' = "Romeo"
charName 's' = "Sierra"
charName 't' = "Tango"
charName 'u' = "Uniform"
charName 'v' = "Victor"
charName 'w' = "Whiskey"
charName 'x' = "X-ray"
charName 'y' = "Yankee"
charName 'z' = "Zulu"
charName 0 = "Zero"
charName 1 = "One"
charName 2 = "Two"
charName 3 = "Three"
charName 4 = "Four"
charName 5 = "Five"
charName 6 = "Six"
charName 7 = "Seven"
charName 8 = "Eight"
charName 9 = "Nine"
charName x = ""

它给了我以下错误:

  

[1/1]编译Main(baby.hs,解释)

     

baby.hs:41:9:       无法匹配预期类型a' against inferred type Char'         在baby.hs:40:12 a' is a rigid type variable bound by the type signature for charName'       在模式中:'a'       在`charName'的定义中:charName'a'=“Alpha”

     

baby.hs:67:9:       没有(Num Char)的实例         由文字0' at baby.hs:67:9 Possible fix: add an instance declaration for (Num Char) In the pattern: 0 In the definition of charName'引起:charName 0 =“Zero”   失败,模块加载:无。

我不知道如何才能让它发挥作用。有没有人有任何想法?

3 个答案:

答案 0 :(得分:11)

使用新数据类型

将Char或Int作为函数参数传递的简单方法是定义一个新的数据类型来封装它们:

data (Num a) => CharOrNum a = C Char | N a

charName (C 'z') = "Zulu"
charName (N 0) = "Zero"

然后你可以像

一样使用它
ghci> charName $ C 'z'
"Zulu"
ghci> charName $ N 0
"Zero"

通过此更改,charName的类型为(Num t) => CharOrNum t -> [Char]

使用新类型

另一种方法是为两个参数类型定义一个公共类型类,例如Show

class Nameable a where
  nameit :: a -> String

instance Nameable Char where
  nameit 'z' = "Zulu"
  nameit _ = ""

instance Nameable Integer where
  nameit 0 = "Zero"
  nameit _ = ""

然后你可以像这样使用它:

ghci> (nameit 0, nameit 'z')
("Zero","Zulu")

答案 1 :(得分:5)

charName的不同情况下的参数类型不匹配。有时您使用Char(例如'a'),有时您会使用数字(例如9)。

您无法通过更改类型签名来完成此工作。 (嗯,有一种方法:添加一个实例Num Char,但这是一个非常糟糕的主意。)

实现您打算做的唯一明智的方法是将数字更改为Char s(即'0'而不是0等。)

答案 2 :(得分:2)

嗯,你必须决定参数的类型。 Char还是Int? charName 'a'charName 'z'将Char作为参数。 charName 0charName 9取一个Int。而charName x需要......好吧,任何类型。

我将charName 0更改为charName '0'等等 并使用charName _ = ""匹配除列出的其他单个Char:

...
charName 'y' = "Yankee"
charName 'z' = "Zulu"
charName '0' = "Zero"
...
charName '9' = "Nine"
charName _ = ""

通过此更改,函数类型为:charName :: Char - >串