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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:48:05+00:00 2026-05-10T22:48:05+00:00

As I continue my quest of learning functional programming, I’ve come to wonder if

  • 0

As I continue my quest of learning functional programming, I’ve come to wonder if there may be alternatives to my default ‘procedural’ way of thinking. To be more specific, I’m looking at a function I wrote. Here is what it does:

Swap two elements of an unordered list of numbers, such that one of the elements    is now in the right place Add the sum of the swapped values to an accumulated total    Repeat until list is sorted 

So, right now I’m using a standard loop* with an accum variable to do the above. It works fine and all, and there’s certainly nothing wrong with iteration in real life, but as the point of this exercise is to expand my way of thinking, I’m curious if there is a more functional approach to the above algorithm.

Thanks!

*(Actually recursion, but whatever)

  • 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. 2026-05-10T22:48:06+00:00Added an answer on May 10, 2026 at 10:48 pm

    From EigenClass:

    The venerable master Leroy was walking with his student. Wishing to start a discussion with his master, the apprentice said ‘Master, I’ve heard that all loops must be replaced with tail-recursive functions. Is that true?’ Leroy looked commiseratively at his student and replied ‘Foolish pupil, many tail-recursive functions are merely inefficient loops.’

    The student spent the next few weeks replacing tail-recursive functions with explicit loops. He finally showed his code to master Leroy, seeking his approval. Leroy hit him with a stick. ‘When will you learn? Explicit loops are a poor man’s tail-recursive functions.’ At that moment, the student became enlightened.

    Edit: Referring to Xavier Leroy, primary developer of OCaml

    Since I can’t see your function to understand how functional* it is, I don’t know. But it seems like what you are doing is correct. My main suggestion would be looking at data structures that lend themselves well to functional programming –but you are using lists, so that’s out, although, lists aren’t the best data structure in this case. As well as the algorithm. If you are pigeon holed into using the insertion sort, then you might not be able to use merge sort or other more efficient methods.

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

Sidebar

Related Questions

No related questions found

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.