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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:46:46+00:00 2026-06-12T05:46:46+00:00

I have a very simple MergeSort implementation on List. /// Divide the list into

  • 0

I have a very simple MergeSort implementation on List.

/// Divide the list into (almost) equal halves
let rec split = function
    | [] -> [], []
    | [x] -> [x], []
    | x1::x2::xs -> let xs1, xs2 = split xs
                    x1::xs1, x2::xs2

/// Merge two sorted lists
let rec merge xs ys =
    match xs, ys with
    | [], _ -> ys
    | _, [] -> xs
    | x::xs', y::ys' when x <= y -> x::merge xs' ys
    | _, y::ys' -> y::merge xs ys' 

let rec mergeSort = function
    | [] -> []
    | xs -> let xs1, xs2 = split xs
            merge (mergeSort xs1) (mergeSort xs2)

But whenever I tried to test with any input in F# Interactive:

let xs = mergeSort [1;4;3;2];;

I encountered a value restriction error:

error FS0030: Value restriction. The value ‘xs’ has been inferred to
have generic type
val xs : ‘_a list when ‘_a : comparison Either define ‘xs’ as a simple data term, make it a function with explicit arguments or, if
you do not intend for it to be generic, add a type annotation.

Why does it happen? What is a simple way to fix it?

  • 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-12T05:46:48+00:00Added an answer on June 12, 2026 at 5:46 am

    You are not handling the special case of 1-element lists in mergeSort.
    The general case is “too general” to infer the right type. As a consequence, the compiler infers a too generic type for the function (‘a list -> ‘b list) and the result is always a generic list (which is not allowed due to value restriction).

    If you fix it like this, the type will be correctly inferred as ‘a list -> ‘a list.

    let rec mergeSort = function
        | [] -> []
        | [x] -> [x]
        | xs -> let xs1, xs2 = split xs
                merge (mergeSort xs1) (mergeSort xs2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple jQuery function: $(.milestone-in-tree).live({ mouseenter: function() { setTimeout( $.ajax({ type:
I have very simple code: List<String> list = new ArrayList<String>(); String a = a;
I have very simple code and I am not sure why the Foo function
I have a very simple list of objects that represt people. Each object has
Let's say we have very simple Java class MyClass . public class MyClass {
I have very simple php interface: interface HasFormattableNumber { function setNumber(); function setFormat(); function
I have very simple problem. I made a very simple function for you to
I have very simple example of an un-ordered list with a black border, and
I have very simple piece of code. The goal is when i input four-digit
I have very simple OpenGL ES example similar to Hehe's example : http://nehe.gamedev.net/tutorial/ios_lesson_02__first_triangle/50001/ As

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.