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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:36:07+00:00 2026-05-25T11:36:07+00:00

UPDATE – Solution Thanks to jacobm for his help, I came up with a

  • 0

UPDATE – Solution

Thanks to jacobm for his help, I came up with a solution.

// Folding Recursion
let reverse_list_3 theList = 
    List.fold_left (fun element recursive_call -> recursive_call::element) [] theList;;

I’m learning about the different ways of recursion in OCaml (for class) and for some exercise, I’m writing a function to reverse a list using different recursion styles.

// Forward Recursion
let rec reverse_list_forward theList =
    match theList with [] -> [] | (head::tail) -> (reverse_list_1 tail) @ [head];;

// Tail Recursion
let rec reverse_list_tail theList result =
    match theList with [] -> result | (head::tail) -> reverse_list_2 tail (head::result);;

Now, I’m trying to write a reverse function using List.fold_left but I’m stuck and can’t figure it out. How would I write this reverse function using folding?

Also, if anyone has good references on functional programming, the different types of recursion, higher-order-functions, etc…, links would be greatly appreciated 🙂

  • 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-25T11:36:08+00:00Added an answer on May 25, 2026 at 11:36 am

    I find it helpful to think of the fold operations as a generalization of what to do with a sequence of operations

    a + b + c + d + e
    

    fold_right (+) 0 applies the + operation right-associatively, using 0 as a base case:

    (a + (b + (c + (d + (e + 0)))))
    

    fold_left 0 (+) applies it left-associatively:

    (((((0 + a) + b) + c) + d) + e)
    

    Now consider what happens if you replace + with :: and 0 with [] in both right- and left-folds.


    It may also be useful to think about the way fold_left and fold_right work as “replacing” the :: and [] operators in a list. For instance, the list [1,2,3,4,5] is really just shorthand for 1::(2::(3::(4::(5::[])))). It may be useful to think of fold_right op base as letting you “replace” :: with op and [] with base: for instance

    fold_right (+) 0 1::(2::(3::(4::(5::[]))))
    

    becomes

    1 + (2 + (3 + (4 + (5 + 0))))
    

    :: became +, [] became 0. From this perspective, it’s easy to see that fold_right (::) [] just gives you back your original list. fold_left base op does something a bit weirder: it rewrites all the parentheses around the list to go the other direction, moves [] from the back of the list to the front, and then replaces :: with op and [] with base. So for instance:

    fold_left 0 (+) 1::(2::(3::(4::(5::[]))))
    

    becomes

    (((((0 + 1) + 2) + 3) + 4) + 5)
    

    With + and 0, fold_left and fold_right produce the same result. But in other cases, that’s not so: for instance if instead of + you used - the results would be different: 1 – (2 – (3 – (4 – (5 – 0)))) = 3, but (((((0 – 1) – 2) – 3) – 4) – 5) = -15.

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

Sidebar

Related Questions

UPDATE: Thanks for all your help guys! I just need to take a litte
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:
Update 2: thanks again to @deepak-azad, I managed to solve my problem : here
UPDATE: I now have a solution I'm much happier with that, whilst not solving
Update: Thanks for everyone who helped out - the answer to this one lay
[UPDATE] Was just an idiot mistake. See end for solution. I am trying to
UPDATE: Solution at bottom. I recently switched from using a set up such as
UPDATE: Thanks for the answers! I can't seem to figure it out, even with
Update: I am sorry i pasted in list.xml twice by mistake, my question is

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.