Skip to content

Instantly share code, notes, and snippets.

@jcamenisch
Created January 7, 2016 00:38
Show Gist options
  • Save jcamenisch/26609658a5038f5d4fb5 to your computer and use it in GitHub Desktop.
Save jcamenisch/26609658a5038f5d4fb5 to your computer and use it in GitHub Desktop.
defmodule Chop do
def guess(actual, low..high) do
n = div(low + high, 2)
IO.puts "Is it #{n}?"
_next_guess(actual, low..high, n)
end
defp _next_guess(actual, _.._, actual) do
actual
end
defp _next_guess(actual, low.._, n) when n > actual do
guess(actual, low..n)
end
defp _next_guess(actual, _..high, n) when n < actual do
guess(actual, n..high)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment