[erlang-questions] conditional expressions
Richard O'Keefe
ok@REDACTED
Tue Nov 18 04:20:55 CET 2008
On 15 Nov 2008, at 1:38 pm, damien morton wrote:
> I keep wanting to have python-like boolean expression evaluation, in
> which a value of nil, 0, an empty list or an empty tuple are
> considered to be false.
You could do it via a macro that expanded
?PY_OR(e1, e2)
to
case py_val(e1)
of {X} -> X
; false -> e2
end
where
py_val(undef) -> false; % Closer match for Undef than nil is
py_val(0) -> false;
py_val(0.0) -> false;
py_val([]) -> false;
py_val({}) -> false;
py_val(false) -> false;
py_val(X) -> {X}.
However, this really is not very Erlang-like.
(For one thing, it's not very Dialyser-friendly.)
Lisp had a single value used to represent logical falsehood (NIL),
the empty list ('() => NIL), and a three-letter atom ('NIL => NIL).
Scheme found it very useful to separate them (#f, '(), and 'NIL
are all different in Scheme). Erlang is closer to Scheme here.
It may be that you are trying to write Python in Erlang,
which isn't the best way to use Erlang.
It may also be that you don't have (m)any cases where
ALL of the 'false' values listed above are actually possible,
just many cases where one of them is possible and you would
like to use the same operator for all of them. Well, 'case'
is definitely the construct you want.
Can you provide some examples?
More information about the erlang-questions
mailing list