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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:49:45+00:00 2026-06-11T02:49:45+00:00

This is how I implemented merge-sort in F# using imperative style: let merge (l1:

  • 0

This is how I implemented merge-sort in F# using imperative style:

let merge (l1: List<string>,  l2: List<string>) = 
 let r: List<string> = new List<string>()
 let mutable (i,j, cnt1, cnt2) =  (0,0, l1.Count, l2.Count)
 while i < cnt1 && j < cnt2 do
    if l1.[i] <= l2.[j] then
        r.Add (l1.[i])
        i <- i + 1
    else
        r.Add (l2.[j])
        j <- j + 1

 if i = cnt1 then
    while j < cnt2 do
        r.Add (l2.[j])
        j <- j + 1
 else    
    while i < cnt1 do
        r.Add (l1.[i])
        i <- i + 1
 r 

Can you convert this to alternate ‘functional’ styled implementation and explain how it works, if possible? Even though I am studying list comprehensions and all that at the moment, I can’t come up with an idea to use it here.

  • 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-11T02:49:47+00:00Added an answer on June 11, 2026 at 2:49 am

    You’re using .NET List<'T> which is renamed to ResizeArray<‘T> in F# to avoid confusion. If you use functional list, merge function would look like this:

    let merge(xs, ys) =
        let rec loop xs ys acc =
            match xs, ys with
            | [], [] -> List.rev acc (* 1 *)
            | [], y::ys' -> loop xs ys' (y::acc) (* 2 *)
            | x::xs', [] -> loop xs' ys (x::acc) (* 3 *)
            | x::xs', y::_ when x <= y -> loop xs' ys (x::acc) (* 4 *)
            | _::_, y::ys' -> loop xs ys' (y::acc) (* 5 *)
        loop xs ys []
    

    To explain this function in terms of your imperative version:

    • The 4th and 5th patterns are corresponding to the first while loop where you compare two current elements and add the smaller one into a resulting list.
    • The 2nd and 3rd patterns are similar to your 2nd and 3rd while loops.
    • The first pattern is the case where i = cnt1 and j = cnt2 and we should return results. Since a new element is always prepended to the accumulator, we need to reverse it to get a list in the increasing order.

    To be precise, your merge function is just one part of merge-sort algorithm. You need a function to split a list in two halves, call merge-sort on two halves and merge two sorted halves into one. The split function below is left for you as an exercise.

    let rec mergeSort ls =
        match ls with
        | [] | [_] -> ls
        | _  -> let xs, ys = split ls
                let xs', ys' = mergeSort xs, mergeSort ys
                merge(xs', ys')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a simple parallel merge sort algorithm in Java. This cuts the
I am learning Java and algorithms at the same time. I implemented merge sort
In Introduction to Algorithms the merge sort algorithm is implemented with a helper function
I just have implemented a threaded version of the merge sort. ThreadedMerge.java : http://pastebin.com/5ZEvU6BV
I implemented a parallel merge sort algorithm from Cormen's well-known text. I wrote it
I implemented this Jquery show/hide code and everything works fine except that the box
I've implemented this C# client which communicate with asynchronous connection to another server I've
I've implemented this search algorithm for an ordered array of integers. It works fine
I've implemented this method to return the section header height. However, when the height
I have this code implemented and I like how straight forward it is because

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.