Skip to content

Instantly share code, notes, and snippets.

@robwilliams
Created June 1, 2016 21:36
Show Gist options
  • Save robwilliams/2d1bf8a0f0c0cd2d1d17cbba8cfb6772 to your computer and use it in GitHub Desktop.
Save robwilliams/2d1bf8a0f0c0cd2d1d17cbba8cfb6772 to your computer and use it in GitHub Desktop.
defmodule Chop do
def guess(actual, range = low..high) do
_guess(actual, div(low+high, 2), range)
end
defp _guess(actual, actual, _) do
"YOU GOT IT #{actual} it is!"
end
defp _guess(actual, attempt, low.._) when actual < attempt do
guess(actual, low..attempt-1)
end
defp _guess(actual, attempt, _..high) when actual > attempt do
guess(actual, attempt+1..high)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment