Skip to content

Instantly share code, notes, and snippets.

@yaychris
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save yaychris/ec15895d7368e59040a0 to your computer and use it in GitHub Desktop.

Select an option

Save yaychris/ec15895d7368e59040a0 to your computer and use it in GitHub Desktop.
defmodule Chop do
def guess(actual, range) do
midpoint(range)
|> print_guess
|> do_guess(actual, range)
end
defp do_guess(guess, actual, _) when guess == actual do
IO.puts guess
end
defp do_guess(guess, actual, low.._) when guess > actual do
guess(actual, low..(guess - 1))
end
defp do_guess(guess, actual, _..high) when guess < actual do
guess(actual, (guess + 1)..high)
end
defp midpoint(low..high) do
div((high - low), 2) + low
end
defp print_guess(guess) do
IO.puts "Is it #{guess}"
guess
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment