f# 中的`::` 和`@` 有什么区别?
What is the difference between `::` and `@` in 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') 运算符用于连接两个列表。你应该阅读 this topic.
> 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"]
我试图在我的字符串列表中添加一个额外的项目。我首先想到用 ::
.
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') 运算符用于连接两个列表。你应该阅读 this topic.
> 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"]