Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6022213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:49:35+00:00 2026-05-23T03:49:35+00:00

As I was writing this function I knew that I wouldn’t get tail call

  • 0

As I was writing this function I knew that I wouldn’t get tail call optimization. I still haven’t come up with a good way of handling this and was hoping someone else might offer suggestions.

I’ve got a tree:

type Heap<'a> =
| E
| T of int * 'a * Heap<'a> * Heap<'a> 

And I want to count how many nodes are in it:

let count h =
    let rec count' h acc =
        match h with 
        | E -> 0 + acc
        | T(_, value, leftChild, rightChild) ->
            let acc = 1 + acc
            (count' leftChild acc) + (count' rightChild acc)

    count' h 0

This isn’t isn’t optimized because of the addition of the counts for the child nodes. Any idea of how to make something like this work if the tree has 1 million nodes?

Thanks, Derek


Here is the implementation of count using CPS. It still blew the stack though.

let count h =
    let rec count' h acc cont =
        match h with
        | E -> cont (1 + acc)
        | T(_,_,left,right) ->
            let f = (fun lc -> count' right lc cont)
            count' left acc f

    count' h 0 (fun (x: int) -> x)

Maybe I can come up with some way to partition the tree into enough pieces that I can count without blowing the stack?

Someone asked about the code which generates the tree. It is below.

member this.ParallelHeaps threads =
    let rand = new Random()
    let maxVal = 1000000

    let rec heaper i h =
        if i < 1 then
            h
        else
            let heap = LeftistHeap.insert (rand.Next(100,2 * maxVal)) h
            heaper (i - 1) heap

    let heaps = Array.create threads E
    printfn "Creating heap of %d elements, with %d threads" maxVal threads
    let startTime = DateTime.Now
    seq { for i in 0 .. (threads - 1) ->
          async { Array.set heaps i (heaper (maxVal / threads) E) }}
    |> Async.Parallel
    |> Async.RunSynchronously 
    |> ignore

    printfn "Creating %d sub-heaps took %f milliseconds" threads (DateTime.Now - startTime).TotalMilliseconds
    let startTime = DateTime.Now

    Array.length heaps |> should_ equal threads <| "The size of the heaps array should match the number of threads to process the heaps"

    let rec reMerge i h =
        match i with 
        | -1 -> h
        | _  -> 
            printfn "heap[%d].count = %d" i (LeftistHeap.count heaps.[i])
            LeftistHeap.merge heaps.[i] (reMerge (i-1) h)

    let heap = reMerge (threads-1) E
    printfn "Merging %d heaps took %f milliseconds" threads (DateTime.Now - startTime).TotalMilliseconds
    printfn "heap min: %d" (LeftistHeap.findMin heap)

    LeftistHeap.count heap |> should_ equal maxVal <| "The count of the reMerged heap should equal maxVal"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:49:35+00:00Added an answer on May 23, 2026 at 3:49 am

    You can use continuation-passing style (CPS) to solve that problem. See Recursing on Recursion – Continuation Passing by Matthew Podwysocki.

    let tree_size_cont tree = 
      let rec size_acc tree acc cont = 
        match tree with 
        | Leaf _ -> cont (1 + acc) 
        | Node(_, left, right) -> 
             size_acc left acc (fun left_size -> 
             size_acc right left_size cont) 
    
      size_acc tree 0 (fun x -> x)
    

    Note also that in Debug builds, tail call optimization is disabled. If you don’t want to run in Release mode, you can enable the optimization in the project’s properties in Visual Studio.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I am writing a searching function, and have thought up of this query using
I'm writing this question in the spirit of answering your own questions, since I
I'm writing this question from the standpoint of an ASP.NET application. However I realize
I am writing this java program to find all the prime numbers up to
I'm writing this question with reference to this one which I wrote yesterday. After
I was wondering if anyone here had some experience writing this type of script
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I'm writing code like this, doing a little quick and dirty timing: var sw
I frequently find myself writing code like this: List<int> list = new List<int> {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.