list - 我将集合转换为列表的 Haskell 程序出现错误
问题描述
我是一个初学者程序员,我已经为我的集合创建了一个数据类型。然后我编写了一个函数将它们转换为列表,但我不断收到以下表达式错误消息:
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
解决方案
问题是它toList 3 :-: 4 :-: 5 :-: Empty
被解析为(toList 3) :-: 4 :-: 5 :-: Empty
,但你实际上想要toList (3 :-: 4 :-: 5 :-: Empty)
。要使其工作,您需要编写后者,或使用$
,如下所示:toList $ 3 :-: 4 :-: 5 :-: Empty
.
推荐阅读
- python - Python3- Geany:从 url 列出 csv 中超出范围的索引
- ionic-framework - 当 base64 值作为 URL 而不是文件路径或文件 url 给出时,离子流媒体不起作用
- c - 迭代结构成员,这些成员是 C 中的数组
- c#-9.0 - 在 C# 9.0 的 switch-case 中更新多个变量
- python - 如何在没有 pip 的情况下从源代码安装 Python 模块
- python - 使用python selenium进行网页抓取,一次登录成功,下次尝试失败
- spring-boot - Spring Boot 检测缺失的翻译
- vb.net - 要打印的面板内的 Tablelayoutpanel 位置
- java - 在java上删除电话簿中的联系人
- python - 给定嵌套字典时如何设置多个变量?(Python)