Created
July 29, 2014 01:36
-
-
Save chrismckelt/be94d8d958dd4353185b to your computer and use it in GitHub Desktop.
F# Binary Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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