【发布时间】:2018-10-24 20:47:06
【问题描述】:
在这个示例中,我无法理解 F# 的 List 和 Seq 之间的区别。我认为主要区别在于 Seq 有点懒惰,但我一定错过了一些东西。
这段代码sn-p:
open System.Collections.Generic
let arr =
["a"; "b"; "c"]
|> Seq.map (fun a -> let dic = Dictionary () in dic.Add("key", a); dic) in
arr
|> Seq.iter (fun a ->
printfn "here";
a.["key"] <- "something"
);
arr
|> Seq.iter (fun a -> printfn "%s" a.["key"])
给予
here
here
here
a
b
c
而(将第一个 Seq 替换为 List)
open System.Collections.Generic
let arr =
["a"; "b"; "c"]
|> List.map (fun a -> let dic = Dictionary () in dic.Add("key", a); dic) in
arr
|> Seq.iter (fun a ->
a.["key"] <- "something"
);
arr
|> Seq.iter (fun a -> printfn "%s" a.["key"])
给予
something
something
something
为什么我使用 Seq 时 Dictionary 的值没有改变?在打印here 时,元素被清楚地访问。
提前致谢。
【问题讨论】:
标签: f#