function - “func :: String -> [Int]; func = read "[3,5,7]" 有什么问题?
问题描述
在一个非常简单的模块test
中,我具有以下功能
func :: String -> [Int]
func = read "[3,5,7]"
由于我有明确的类型注释,我希望[3,5,7]
在加载模块test
并调用func
ghci 时得到。然而,我得到了
• No instance for (Read (String -> [Int]))
arising from a use of ‘read’
(maybe you haven't applied a function to enough arguments?)
• In the expression: read "[3,5,7]"
In an equation for ‘func’: func = read "[3,5,7]"
|
11 | func = read "[3,5,7]"
| ^^^^^^^^^^^^^^
但是当我这样做时read "[3,5,7]" :: [Int]
,[3,5,7]
按预期返回。为什么在我加载模块时会引发错误?
解决方案
您正在尝试将您的字符串作为类型的函数String -> [Int]
而不是列表来读取[Int]
。但是,read
不能将字符串转换为函数。
试试这个:
myList :: [Int]
myList = read "[3,5,7]"
推荐阅读
- elasticsearch - 在 Kafka Connect 分布式模式下为多个主题配置连接器
- reactjs - 反应路由器不加载组件
- ruby-on-rails - Active Admin 不会安装:StringIO 类的超类不匹配(TypeError)
- sql - 在 2 个表上使用 UNION ALL 以获得特定结果
- matlab - MATLAB 神经网络:输入数量与 net.numInputs 不匹配
- javascript - 如何添加 if 语句来检查函数中的多个变量(Javascript)
- ios - 约束被系统停用
- r - 如何基于两个变量合并两个数据框?
- javascript - Javascript onmouseenter & onmouseleave
- typescript - “文本”类型上不存在属性“then”