在 Windows 7 上的 F# 3.1 上测试
fsi.PrintLength <- 5000;;
[1..5000];;
Process is terminated due to StackOverflowException. Session termination detected. Press Enter to restart.
在 Mono (F# 4.0) 上,似乎没有这样的限制。
最佳答案
我认为这是 formatting module 中的一个错误它负责将 pretty-print 到 F# Interactive。
有一些非尾递归函数使用 PrintLength
例如boundedUnfoldL
在此 line . boundedUnfoldL
的实现确实不是尾递归的:
let boundedUnfoldL
(itemL : 'a -> layout)
(project : 'z -> ('a * 'z) option)
(stopShort : 'z -> bool)
(z : 'z)
maxLength =
let rec consume n z =
if stopShort z then [wordL "..."] else
match project z with
| None -> [] // exhaused input
| Some (x,z) -> if n<=0 then [wordL "..."] // hit print_length limit
else itemL x :: consume (n-1) z // cons recursive...
consume maxLength z
我不知道为什么它不会在 Mono 上爆炸。如果 F# Interactive on Mono 可以成功处理长度 > 5000,那将是令人惊讶的。
您可以将此作为错误报告给 https://visualfsharp.codeplex.com/workitem/list/basic .
关于f# - 为什么在 F# Interactive 中打印 5000 个数字会导致 StackOverflowException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27432657/