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

  • SEARCH
  • Home
  • 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 3358306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:44:13+00:00 2026-05-18T02:44:13+00:00

As You may know, there are higher order functions in OCaml, such as fold_left,

  • 0

As You may know, there are higher order functions in OCaml, such as fold_left, fold_right, filter etc.

On my course in functional programming had been introduced function named fold_tree, which is something like fold_left/right, not on lists, but on (binary) trees. It looks like this:

 let rec fold_tree f a t = 
  match t with
    Leaf -> a |
    Node (l, x, r) -> f x (fold_tree f a l) (fold_tree f a r);;

Where tree is defined as:

type 'a tree = 
  Node of 'a tree * 'a * 'a tree | 
  Leaf;;

OK, here’s my question: how does the fold_tree function work? Could you give me some examples and explain in human language?

  • 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-18T02:44:14+00:00Added an answer on May 18, 2026 at 2:44 am

    Here’s a style suggestion, put the bars on the beginning of the line. It makes it clearer where a case begins. For consistency, the first bar is optional but recommended.

    type 'a tree = 
      | Node of 'a tree * 'a * 'a tree
      | Leaf;;
    
    let rec fold_tree f a t = 
        match t with
          | Leaf -> a
          | Node (l, x, r) -> f x (fold_tree f a l) (fold_tree f a r);;
    

    As for how it works, consider the following tree:

    let t = Node(Leaf, 5, Node(Leaf, 2, Leaf));;
    

    With the type int tree.

    Visually, t looks like this:

       5
      / \
     ()  2
        / \
       () ()
    

    And calling fold_tree, we’d need a function to combine the values. Based on the usage in the Node case, f takes 3 arguments, all the same type of the tree’s and returns the same. We’ll do this:

    let f x l r = x + l + r;; (* add all together *)
    fold_tree f 1 t;;
    

    It would help to understand what happens in each case. For any Leaf, a is returned. For any Node, it combines the stored value and the result of folding the left and right subtrees. In this case, we’re just adding the numbers where each leaf counts as one. The result on the folding of this tree is 10.

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

Sidebar

Related Questions

for website that embed flash to capture camera (red5..etc). may i know is there
May I know if there is any way to remove non-alphabetical symbols from a
As you may know, in many occasions, there is a need to flag some
This may be a silly question I don't know. Is there a way to
May I know if there are any issues running a Java 5-compiled code (with
May I know is there any way to determine parent class from boost::any? #include
May I know is there any plug-in for eclipse with which I can access
I am working on files n folder using C#....May I know is there any
May I know is there any C++ equivalent class, to Java java.util.concurrent.ArrayBlockingQueue http://download.java.net/jdk7/docs/api/java/util/concurrent/ArrayBlockingQueue.html
as shown above may i know is there a way to check a return

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.