Last active
August 29, 2015 14:23
-
-
Save gtcarlos/0222f987ec38f577894f to your computer and use it in GitHub Desktop.
Guess exercise in Elixir from PragProg's Programming Elixir book
This file contains 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 Chop do | |
def guess(actual, range) do | |
low..high = range | |
mid = div(low + high, 2) | |
IO.puts("Is it #{mid}") | |
_guess(actual, low, high, mid) | |
end | |
defp _guess(actual, low, _high, mid) when actual < mid, do: guess(actual, low..mid) | |
defp _guess(actual, _low, high, mid) when actual > mid, do: guess(actual, mid..high) | |
defp _guess(actual, _low, _high, mid) when actual == mid, do: IO.puts(mid) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment