【发布时间】:2020-04-21 15:28:44
【问题描述】:
我是一名初级程序员,我已经为我的集合创建了一个数据类型。然后我编写了一个函数将它们转换为列表,但我不断收到以下表达式错误消息:
toList 3 :-: 4 :-: 5 :-: Empty
Non type-variable argument in the constraint: Num (Set a)
(Use FlexibleContexts to permit this)
When checking the inferred type
it :: forall a. (Num (Set a), Num [a]) => Set [a]
这是我的代码:
infixr 5 :-:
data Set a = Empty | a :-: (Set a) deriving (Show, Read, Eq, Ord)
toList :: Set a -> [a]
toList Empty = []
toList (x :-: xs) = x : toList xs
【问题讨论】:
-
我无法复制。这段代码根本没有提到
Num,所以不清楚你的错误来自哪里。 -
我尝试将函数 toList 应用于集合 3 :-: 4 :-: 5 :-: 空。不知道有没有用?
-
请发布您在 GHCi 中输入的确切文本
-
toList 3 :-: 4 :-: 5 :-: 空
-
这被解析为
(toList 3) :-: 4 :-: 5 :-: Empty,所以4成为Set a的参数:-:。请改用toList (3 :-: 4 :-: 5 :-: Empty)。