The question is how often do you find yourself reaching for an if? In Erlangif works like this:
is_greater_than(X, Y) ->
if
X>Y ->
true;
true -> % works as an 'else' branch
false
end
It makes it abundantly clear that in a functional language if simply selects one of two possible expressions/values.
In imperative programming if is typically used to specify separate statement blocks each representing mutually exclusive execution paths which is a bit different - leading sometimes to extreme positions like the Anti-IF campaign.
Functional programming languages are expression based - not statement based.
defp build_if(condition, do: do_clause) do
build_if(condition, do: do_clause, else: nil)
end
defp build_if(condition, do: do_clause, else: else_clause) do
optimize_boolean(quote do
case unquote(condition) do
x when x in [false, nil] -> unquote(else_clause)
_ -> unquote(do_clause)
end
end)
end
So in a way the special form cond/1 is closer to the spirit of a conventional if - but it’s still an expression (and hence the (at least initially) odd looking true -> branch).