list - f#中的`::`和`@`有什么区别?
问题描述
我试图在我的字符串列表中添加一个额外的项目。我首先想到将项目添加到我的列表中::
。
let test = ["hello"];;
let newtest = test :: ["world"];;
这给我带来了错误:
let newtest = test :: ["world"];;
-----------------------^^^^^^^
stdin(36,24): error FS0001: This expression was expected to have type
'string list'
but here has type
'string'
它才开始与@
. 但是,在创建新列表的几个 SO 问题上,使用了该::
方法。为了使用::
,我最终创建了一个列表列表,这绝对不是我想要的。
let newtest01 = test :: [["world"]];;
val newtest01 : string list list = [["hello"]; ["world"]]
有人可以解释一下它们之间的区别吗?
解决方案
( ::
'cons') 运算符用于通过在现有列表前添加(或约束)项目来构建列表。( @
'append') 运算符用于连接两个列表。你应该阅读这个主题。
> let test = ["hello"];;
val test : string list = ["hello"]
> let newTest1 = "world" :: test;;
val newTest1 : string list = ["world"; "hello"]
> let newTest2 = test @ ["world"];;
val newTest2 : string list = ["hello"; "world"]
推荐阅读
- pandas - 如何将 boto3 StreamingBody 读入 .parquet 文件?
- vue.js - 如何在 Vue 中制作黑色叠加图像
- node.js - Openshift平台上的Nodejs oracle Db连接
- javascript - Angular 10 升级 - 修复 CommonJS 或 AMD 依赖项可能导致优化救助
- javascript - 内联编辑器中的对话流捕获实体
- php - SQLSTATE [23000]:违反完整性限制:1062 'mail@mail.com' 'users_email_unique' 键的重复条目
- php - Alpha vantage API 不适用于 NSE,而同一查询为外国股票提供输出
- java - RefreshAheadCache 不调用 @cacheable 注释方法
- python - 如何为 Tacker 分解实现 mode-k 乘积 Python?
- smtp - SMTP 错误:DATA END 命令失败:550 5.7.1 客户端无权作为此发件人发送