Skip to content

Instantly share code, notes, and snippets.

@loosechainsaw
Created July 28, 2014 13:24
Show Gist options
  • Save loosechainsaw/c1223ce5ad896ab95df4 to your computer and use it in GitHub Desktop.
Save loosechainsaw/c1223ce5ad896ab95df4 to your computer and use it in GitHub Desktop.
Code Dojo 1 - F# Implementation of Binary Search
module Algorithms =
let binary_search list value =
let rec impl(list: 'a list) (value :'a) (hi: int) (low:int) =
let index = (hi + low) / 2
match compare value (list.Item(index)) with
| 0 -> Some(index)
| 1 -> impl list value hi index
| -1 -> impl list value index low
| _ -> None
match list with
| [] -> None
| _ -> impl list value (List.length list) 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment