Problems starting functions using continuation in Visual Studio

I'm currently trying to learn f # using the book Real-World Functional Programming by Petricek and Skeet (2010), but ran into problems when using continuations to avoid.

The problem I am facing is that my code using continuations works fine when run in interactive f #, but still causes a stack overflow when I put the code in the program.fs file and then run it through the debugger in Visual Studio,

It is not clear to me why this is happening, and would greatly appreciate if someone can explain to me why this is happening.

If the Visual Studio version is up to date, I am using: Visual Studio Ultimate 2012 Version 11.0.61030.00 Update 4

.Net framework used: Version. 4.5.51641

Below is the code provided in the book that is causing this problem:

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)

      

Thanks in advance! // Tigerstrom

+3


source to share


1 answer


If you run the program in debug mode, the use of tail calls is disabled by default in the Visual Studio project settings. The main reason is that with tail permissions enabled, you don't get very useful information in the call stack (which makes debugging more difficult).



To fix this, you can go to your project options and check the Generate Tail Calls on the Create page. This is enabled by default in release mode.

+3


source







All Articles