Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
defmodule BinarySearch do
def find(item, _left, [i | _right]) when i == item, do: true
def find(item, _left, [i | right]) when i < item, do: find(item, right)
def find(item, left, [i | _right]) when i > item, do: find(item, left)
def find(_item, _left, []), do: false
def find(item, list) do
{left, right} = split(list)
find(item, left, right)
end
def split(list) do
len = round(length(list) / 2)
case Enum.split(list, len) do
{left, []} -> {[], left}
res -> res
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment