I was creating simple RPN calculator function, but I stumbled upon a problem in matching functions in guards.
Code looks like so:
def compute_RPN_expr(value, stack) when is_number(value), do: [value | stack]
def compute_RPN_expr(operator, stack) when operator in [&+/2, &-/2, &*/2, &//2] do
[a, b | stack] = stack
stack = [operator.(b, a) | stack]
end
After trying to define it inside IEX I’m getting invalid expression in guard, & is not allowed in guards.
Why? I can do:
a = &//2
a === &//2
Without much hassle, but I can’t use the same equality test via in
.
Is there any Elixir/Erlang limitation or this was simply overlooked?