Created
May 28, 2021 20:04
-
-
Save hovsater/43532cbd3530693777bcdce34bbe40fb to your computer and use it in GitHub Desktop.
Guess the number!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Guess do | |
def make_guess(n, low..high), do: make_guess(n, low..high, div(low + high, 2)) | |
defp make_guess(n, _range, guess) when guess == n, do: IO.puts(n) | |
defp make_guess(n, _low..high, guess) when guess < n do | |
with low = guess + 1, | |
guess = div(low + high, 2) do | |
IO.puts("Is it #{guess}") | |
make_guess(n, low..high, guess) | |
end | |
end | |
defp make_guess(n, low.._high, guess) when guess > n do | |
with high = guess - 1, | |
guess = div(low + high, 2) do | |
IO.puts("Is it #{guess}") | |
make_guess(n, low..high, guess) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment