1

我想将子数据类型传递给函数。

一个例子:

data Car a = Ford | Opel | BMW | Mercedes a deriving (Show)

data Mercedes a = SClass a| CClass a| MClass a


myfunc::Car->String
myfunc a = show a ++ " works correctly"

但是我愿意:

myfunc CClass "x"

它说

不在范围内:构造函数 CClass

4

2 回答 2

11

要实现“子”类型,有几种选择。第一个只是使用更多data类型,因为您正在尝试:

data Car a = Ford (Ford a) | Mercedes (Mercedes a) deriving (Show)

data Ford a = ModelT a | Falcon a deriving (Show) -- different Fords models
data Mercedes a = CClass a | SClass a deriving (Show) -- different Mercedes models

请注意,作为类型构造函数与作为数据构造函数不同,因此不能只使用data Car = .. | Mercedes a:这是说制作 a 的方法之一Car String就是说Mercedes "a",它与调用的类型无关Mercedes。现在可以使用如下函数:

horsepower :: Car a -> Float
horsepower (Ford (ModelT _)) = 1
horsepower (Ford (Falcon _)) = 500
horsepower (Mercedes (CClass _)) = 300
horsepower (Mercedes (SClass _)) = 400

你的例子是:

myfunc :: Show a => Car a -> String
myfunc a = show a ++ " works correctly"

-- myfunc (Ford (ModelT "A")) => Ford (ModelT "A") works correctly
-- myfunc (Mercedes (CClass "b")) => Mercedes (CClass "b") works correctly

允许像这样的“子类型化”的另一种方法是使用类型类(取决于具体情况,这可能是一个更好的选择)。例如

class (Show c) => Car c where
    make :: String
    model :: c -> String
    horsepower :: c -> Float
    number_of_seats :: c -> Int
    -- etc.

data Ford a = ModelT a | Falcon a deriving (Show)
instance (Show a) => Car (Ford a) where
    make = "Ford"
    horsepower (ModelT _) = 1
    horsepower (Falcon _) = 500
    -- etc.


data Mercedes a = CClass a | SClass a deriving (Show)
instance (Show a) => Car (Mercedes a) where
    make = "Mercedes"
    horsepower (CClass _) = 300
    horsepower (SClass _) = 400
    -- etc.

您的示例myfunc可能是:

myfunc :: (Car c) => c -> String
myfunc c = (make `asTypeOf` c) ++ " " ++ show c ++ " works correctly"

-- myfunc (ModelT "A") => Ford ModelT "A" works correctly
-- myfunc (CClass "b") => Mercedes CClass "b" works correctly

另请注意,必须使用括号调用function (Ford (Ford "a"))(或)之类的函数。function (CClass "a")

于 2012-09-09T15:26:57.430 回答
5

首先,您需要按如下方式定义您的汽车类型:

data Car a = ... | Mercedes (MercedesClass a) | ...

data MercedesClass a = CClass a | ...

...然后你可以写:

myFunc (Mercedes (CClass "x"))
于 2012-09-09T15:16:22.363 回答