让我们考虑以下列表:
// 2 2 2 1 1 1
let xs = [2;2;2;1;1;1]
// 2 2 2 1 1 1
let xs' = List.map (fun x -> x) list
// 4 4 4 1 1 1
let xs'' = List.map (fun x -> x * x) list
Run Code Online (Sandbox Code Playgroud)
会List.map
在第二种情况下分配一个新列表吗?在第三种情况下,将xs
分享尾巴[1;1;1]
用xs''
?
标准库倾向于使用此类函数的简单实现.
结果,没有完成这种类型的优化.
可以在此处找到实现:https://github.com/fsharp/fsharp/blob/master/src/fsharp/FSharp.Core/local.fs#L85