Last active
January 7, 2016 02:01
-
-
Save jcamenisch/6dd7dbe5154dbb879ec9 to your computer and use it in GitHub Desktop.
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, low..high) do | |
next_guess = fn | |
(n) when n == actual -> actual | |
(n) when n > actual -> guess(actual, low..n) | |
(n) when n < actual -> guess(actual, n..high) | |
end | |
n = div(low + high, 2) | |
IO.puts "Is it #{n}?" | |
next_guess.(n) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This next_guess function looks an awful lot like a case statement. If we had been introduced to those yet, I think I would write it as such, and move the block to the end of the function.
Or one could simply invoke the function inline in truly anonymous fashion: