If you want to optimize the callback, this call must be outside the try-catching clause. You can use the construct
your_fun(...) -> ... try ... of <--- make notice of `of` ... -> some_call(...) catch ... end.
or just make this call after a try clause.
In your code that calls has_value1(Val, Right). , optimized because this is the last function call. It does not matter if it is called inside a try block in this case. An exception is used only to provide an early exit from this function and to easily manage the result.
It can be rewritten without exception, but using manual stack processing:
has_value(Val, Tree) -> has_value(Val, [Tree]). has_value1(_, []) -> false; has_value1(Val, [{node, 'nil'} | Stack]) -> has_value1(Val, Stack); has_value1(Val, [{node, {_, Val, _, _}} | _]) -> true; has_value1(Val, [{node, {_, _, Left, Right}} | Stack]) -> has_value1(Val, [Left, Right | Stack]).
Dmitry Belyaev
source share