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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:20:42+00:00 2026-05-18T23:20:42+00:00

So I’ve written a little binary search function (not that I need to, but

  • 0

So I’ve written a little binary search function (not that I need to, but just because I can), and when I make it specific to strings or to integers, for example, it works well. When I attempt to use a generic type signature, I start getting exceptions.

A copy of the function using a generic type, 'a:

let search (needle : 'a) (haystack: 'a array) : int = 
    let length = Array.length haystack

    if Array.length haystack <= 0
    then -1
    else
        let bottom = 0
        let top = Array.length haystack - 1

        let rec search' (needle : 'a) (haystack: 'a array) (bottom:int) (top:int) : int = 
            if bottom = top
            then if needle = haystack.[top] then top else -1
            else
                let middle = (uint32 top + uint32 bottom) >>> 1 |> int  // avoid an overflow

                if needle <= haystack.[middle]
                then search' needle haystack bottom middle
                else search' needle haystack (middle+1) top

        search' needle haystack bottom top 

When this is called, I get the following:

System.InvalidProgramException: Invalid IL code in FSI_0019:search'@921-13<a> (a,a[],int,int): IL_0000: br        IL_0005
  at FSI_0019.search[String] (System.String needle, System.String[] haystack) [0x00000] in <filename unknown>:0 
  at <StartupCode$FSI_0023>.$FSI_0023.main@ () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
Stopped due to error

Am I doing something wrong? (Again, when I use string or int instead of 'a, everything works…)

Edit:

I’ve compiled Mono 2.9 or so, and this function then works in FSI. Now, to wait for Debian and Ubuntu to upgrade… 😀

  • 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-18T23:20:43+00:00Added an answer on May 18, 2026 at 11:20 pm

    Mono bug.
    Before, I had a code that does not work on fsi but works compiled.

    EDIT: It’s confirmed that that code doesnt work on fsi, but works with compiled by fsc.

    • 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.