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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:23:08+00:00 2026-06-09T19:23:08+00:00

While attempting to learn more about sorting algorithims and F#, I wrote an Insertion

  • 0

While attempting to learn more about sorting algorithims and F#, I wrote an Insertion Sort in F#. I am a complete noob to F# and functional programming.

let insert (a: array<int>) i item =
    i = i - 1
    while i >= 0 && item < a.[i] do
        a.[i + 1] = a.[i]
        i = i - 1
    a.[i + 1] = item
    a

let sort (a: array<int>) =
    for i in 1 .. (a.Length - 1) do
        a = insert a i a.[i]
    a
let a = [|3; 4; 1; 3;|]
a = sort a
for i in a do
    printfn "%d" i

The code compiles fine, but when i run the executable…

Unhandled Exception: System.IndexOutOfRangeException: Index was outside the boun
ds of the array.
   at Isort.insert(Int32[] a, Int32 i, Int32 item)
   at Isort.sort(Int32[] a)
   at <StartupCode$isort>.$Isort.main@()

The exception is kind of unhelpful, as it doesn’t say where the out of range exception was…
Is there a way to fix this error in my code?

  • 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-09T19:23:10+00:00Added an answer on June 9, 2026 at 7:23 pm

    Several comments:

    • F# values are immutable by default. If you want to modify values, declare them as mutable.
    • = is equality while <- is assignment to mutable values.
    • Pay attention to warnings in F#. In this case, there are a lot of errors since you ignore values of comparisons.

    An improved version:

    let insert (a: int []) j item =
        let mutable i = j - 1
        while i >= 0 && item < a.[i] do
            a.[i + 1] <- a.[i]
            i <- i - 1
        a.[i + 1] <- item
    
    let sort (b: int []) =
        let a = Array.copy b
        for i in 1 .. (a.Length - 1) do
            insert a i a.[i]
        a
    let a = [|3; 4; 1; 3; 5; 6; 5|]
    let a' = sort a
    for i in a' do printfn "%d" i
    

    I modified names of a few variables for clarity. Moreover, auxiliary function insert could return unit while sort has better return a new copy instead of mutating the input array.

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

Sidebar

Related Questions

I am attempting to implement Heap sort in my program to learn more about
I'm attempting to learn Spring MVC but have been spoiled by Grails. While I
I am attempting to write my own string class (so I can learn more)
While attempting to adopt more TDD practices lately on a project I've run into
I am attempting to learn Java Programming on my own (without classes/teachers/tutors/etc.) so this
While attempting to debug a job submission script, I ended up narrowing down the
While attempting to create a replacement tablespace for USER, I accidentally created a datafile
While attempting to link with QT 4.5 release, I run into this error message
I am attempting to learn how to develop on the Android platform but do
I'm having a strange problem while attempting to transform a blocking socket server into

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.