1

Assume I have a list [A, B, C]. Is there any way I can declare an algebraic data type based on that list with the following semantics:

data V = A | B | C

Thanks!

4

1 回答 1

4

您想要的是动态生成的抽象数据类型,这在任何带有 ADT 的语言中听起来都相当不标准。

您可以做的是拥有一个具有单个构造函数的数据类型,然后是一个所谓的smart-constructor,它对可以传递给构造函数的值施加了一些逻辑。

data PermList a = PermList ([a] -> Bool) [a]

permList :: ([a] -> Bool) -> [a] -> PermList a
permList f xs | f xs      = PermList f xs
              | otherwise = undefined
于 2013-01-24T22:52:45.290 回答