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 8493295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:57:54+00:00 2026-06-10T22:57:54+00:00

I’m trying to learn OCaml, and I’m still struggling a bit with the fold

  • 0

I’m trying to learn OCaml, and I’m still struggling a bit with the fold functions. I’ve done a bit of research and I’ve found the following code snippet (written in Scala) implementing insertion sort using foldleft. I think I get the idea, but I know absolutely nothing about Scala, so I still need a bit of clarification. So, I guess my question is 2-fold (ha, get it?), how would this function be written in Ocaml, and how does it actually work?

def insertionSort[A <% Ordered[A]](list: List[A]): List[A] =
  list.foldLeft(List[A]()) { (r,c) =>
    val (front, back) = r.span(_ < c)
    front ::: c :: back
  }

Thanks for the help!

  • 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-06-10T22:57:56+00:00Added an answer on June 10, 2026 at 10:57 pm

    As I commented above, this isn’t a particularly nice way to sort (assuming I understand the Scala code, which I might not). Here is an OCaml translation of what it seems to be doing:

    let insertion_sort l =
        let add1 sortedl x =
            let le, gt = List.partition ((>=) x) sortedl
            in
                le @ [x] @ gt
        in      
            List.fold_left add1 [] l
    

    After you write FP code for a while, folds become quite natural. The purpose is to process a series of elements from a data structure (e.g., a list) in a known order (e.g., leftmost first), while maintaining some state that gets passed along as you do your processing. In the simplest cases (as here), the state is also your final answer. In other cases, you need to carry some extra state along with the final answer. In those cases you extract the final answer at the end.

    Maybe I shouldn’t say this, but a fold is like the for(;;) statement in the C family, except that it encapsulates all the state that you plan to modify in your loop. In this way, it’s a nicely controlled form of iteration.

    The correspondence looks something like this:

    for(state = S, x = first(D); more(D); x = next(D)) { state = E(state, x) }
    
    fold_left (fun state x -> E state x) S D
    

    The discipline of encapsulating the modified state usually turns out to be incredibly helpful after you get used to it. Very often the function that you fold (like add1 above) turns out to be useful in many other circumstances, because the structure of the fold requires a generally useful function in that spot.

    Note that, in particular, the fold function contains all the knowledge of how to traverse the data structure. The folded function only needs to know about what to do with each element. So you are free to change the data structure (and the fold) without changing any of the other code. And you can also use the same folded function (add1 above) with different data structures.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.