Start..end notation (_min..max) (min.._max)

Can anyone point me to the documentation describing how start…end notation works with the underscore? I.e. When a range is passed to a function as guess(actual, guess, _min..max) vs guess(actual, guess, min.._max).


It depends if You use min or max inside the body of the guess function…

defmodule MyMod do
  def guess(min.._max), do: IO.puts min

iex> MyMod.guess 1..4

If You use min, but not max =>


If You use max, but not min =>


As a..b is syntactic sugar for %Range{first: a, last: b}, min.._max in a pattern match works exactly the same way as %Range{first: min, last: _max}.

In general, the leading _ when binding a name is just a hint for the compiler, that this name won’t be used on purpose.

This avoids warnings like “Variable foo bound but not used”.


Thanks for the answers @kokolegorille and @NobbZ.

I discovered my source of confusion. In Exercise: Modules and Functions-6 (Page 62), the example answer given looks thusly;

defp _guess(actual, guess,  _low..high)
   when​ guess ​<​ actual,
   do​: guess(actual, guess+1..high)


​defp​ _guess(actual, guess,  low.._high)
  when​ guess ​>​ actual,
  do​: guess(actual, low..guess-1)

Which to me makes it seem like _low..high and low.._high are special in other ways than just “ignore this variable”. Or that the compiler matches and executes both functions, which cannot be the case. But once correctly formatted in Emacs the two functions make perfect sense;

  defp _guess(actual, guess, _min..max) when guess < actual,
    do: guess(actual, (guess + 1)..max)
  defp _guess(actual, guess, min.._max) when guess > actual,
    do: guess(actual, min..(guess - 1))