Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Created July 29, 2014 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismckelt/be94d8d958dd4353185b to your computer and use it in GitHub Desktop.
Save chrismckelt/be94d8d958dd4353185b to your computer and use it in GitHub Desktop.
F# Binary Search
module ChrisMcKelt
open System
open System.ServiceModel
open Microsoft.FSharp.Linq
open Microsoft.FSharp.Data.TypeProviders
open System.Linq
let rec chop (arr:array<int>, low:int, high:int, value:int) =
if (high < low) then
null
else
let mid = (low + high) / 2
if (value > arr.Length) then
box None
else
if (arr.[mid] > value) then
chop (arr, low, mid-1, value)
else if (arr.[mid] < value) then
chop (arr, mid+1, high, value)
else
match box arr.[mid] with
| null -> box None
| x -> box(Some(x))
[<EntryPoint>]
let main argv =
let list = [1..10000]
let findme = 7777
let arr = Array.ofList list
let result = chop(arr, list.Head, list.Last(),findme)
Console.WriteLine(result)
Console.WriteLine("Press any key to continue...");
Console.ReadLine() |> ignore
0 // return an integer exit code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment