I burnt through a bunch of time picking up Go, only to come to the conclusion that I don't really like it.
I've been hearing good things about Elixir and the Phoenix Framework, so I've just started to pick it up. My last exposure to a true functional language was Haskell back about 15 years ago.
I am currently reading "Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun" by Dave Thomas. Good book so far.
Anyway, I just did one of the examples:
Here's my working solution:
Now, this works. However, the author provided a few "hints":
OK, so I am using hints 1-3. The only one I don't understand the need for is hint #4.
Can someone with more Elixir/Functional cred clue me in on how I'd be changing my guard clauses to check against the range? I feel like my solution is pretty straightforward and readable, but I want some opinions.
I've been hearing good things about Elixir and the Phoenix Framework, so I've just started to pick it up. My last exposure to a true functional language was Haskell back about 15 years ago.
I am currently reading "Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun" by Dave Thomas. Good book so far.
Anyway, I just did one of the examples:
I’m thinking of a number between 1 and 1000.…
The most efficient way to find the number is to guess halfway between the low and high numbers of the range. If our guess is too big, then the answer lies between the bottom of the range and one less than our guess. If our guess is too small, then the answer lies between one more than our guess and the end of the range. Your API will be guess( actual, range), where range is an Elixir range. Your output should look similar to this:
iex > Chop.guess( 273, 1.. 1000)
Is it 500
Is it 250
Is it 375
Is it 312
Is it 281
Is it 265
Is it 273
273
Thomas, Dave (2014-10-09). Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun (Kindle Locations 1603-1612). Pragmatic Bookshelf. Kindle Edition.
Here's my working solution:
Code:
defmodule Chop do
# start guessing recursively at the midpoint of the range.
def guess(a, min..max) do
g = div(max - min, 2) + min
IO.puts "Is it #{g}"
r_guess(a, min..max, g)
end
# when guess is equal to actual, return guess.
defp r_guess(a, _, g) when a === g, do: IO.puts g
# when guess is less than actual, move towards the
# max end of range.
defp r_guess(a, _min..max, g) when g < a, do: guess(a, g..max)
# when guess is greater than actual, move towards the
# min end of range.
defp r_guess(a, min.._max, g) when g > a, do: guess(a, min..g)
end
Now, this works. However, the author provided a few "hints":
Hints:
- You may need to implement helper functions with an additional parameter (the currently guessed number).
- The div( a, b) function performs integer division.
- Guard clauses are your friends.
- Patterns can match the low and high parts of a range (a.. b = 4.. 8).
Thomas, Dave (2014-10-09). Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun (Kindle Locations 1615-1618). Pragmatic Bookshelf. Kindle Edition.
OK, so I am using hints 1-3. The only one I don't understand the need for is hint #4.
Can someone with more Elixir/Functional cred clue me in on how I'd be changing my guard clauses to check against the range? I feel like my solution is pretty straightforward and readable, but I want some opinions.