Skip to content

Instantly share code, notes, and snippets.

@gtcarlos
Last active August 29, 2015 14:23
Show Gist options
  • Save gtcarlos/0222f987ec38f577894f to your computer and use it in GitHub Desktop.
Save gtcarlos/0222f987ec38f577894f to your computer and use it in GitHub Desktop.
Guess exercise in Elixir from PragProg's Programming Elixir book
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