F# 在Visual Studio中使用continuation启动函数时出现的问题
我目前正在尝试使用Petricek和Skeet(2010)的《真实世界函数编程》一书学习f#,但在使用continuations避免堆栈溢出时遇到了问题 我遇到的问题是,当在f#interactive中启动时,我使用continuations的代码可以完美地工作,但当将代码放置在program.fs文件中,然后通过Visual Studio中的调试器启动时,仍然会导致堆栈溢出 我不清楚为什么会发生这种情况,如果有人能给我解释一下为什么会发生这种情况,我将不胜感激 如果Visual Studio的版本相关,我将使用: Visual Studio Ultimate 2012 版本11.0.61030.00更新4 使用的.Net framework是: 版本4.5.51641 本书中出现的导致此问题的代码如下所示:F# 在Visual Studio中使用continuation启动函数时出现的问题,f#,continuations,F#,Continuations,我目前正在尝试使用Petricek和Skeet(2010)的《真实世界函数编程》一书学习f#,但在使用continuations避免堆栈溢出时遇到了问题 我遇到的问题是,当在f#interactive中启动时,我使用continuations的代码可以完美地工作,但当将代码放置在program.fs文件中,然后通过Visual Studio中的调试器启动时,仍然会导致堆栈溢出 我不清楚为什么会发生这种情况,如果有人能给我解释一下为什么会发生这种情况,我将不胜感激 如果Visual Studio的
open System
let rand = new Random()
//A tree is either a leaf with a value or a node that contains two children
type IntTree =
| Leaf of int
| Node of IntTree * IntTree
//A list of that will decide all leaf values
let numbers2 = List.init 1000000 (fun _ -> rand.Next(-50,51))
///Creates an imbalanced tree with 1000001 leafs.
let imbalancedTree2 =
numbers2 |> List.fold (fun currentTree num ->
Node(Leaf(num), currentTree)) (Leaf(0))
//Sums all leafs in a tree by continuation and will execute the inserted function with the total
//sum as argument once all values have been summed.
let rec sumTreeCont tree cont =
match tree with
| Leaf(num) -> cont(num)
| Node(left, right) ->
sumTreeCont left (fun leftSum ->
sumTreeCont right (fun rightSum ->
cont(leftSum + rightSum)))
//Sums the imbalanced tree using the sumTreeCont function
let sumOfTree = sumTreeCont imbalancedTree2 (fun x -> x)
提前谢谢!
//如果在调试模式下运行程序,则默认的Visual Studio项目设置将禁用尾部调用。主要原因是,启用了尾部调用后,在调用堆栈中无法获得非常有用的信息(这会使调试更加困难)
要解决此问题,您可以转到项目选项并在“构建”页面上选中“生成尾部调用”。在发布模式下,这是默认启用的。非常感谢,这解决了问题!我还要感谢你写了一本好书。这是一本非常有教育意义的书,内容非常丰富!谢谢,我很高兴你喜欢这本书:-)