S
Steven D'Aprano
I think someone already beat you to it. They call their invention
"Lisp".
Also Forth.
I think someone already beat you to it. They call their invention
"Lisp".
Consider the following code :
# --------------------------------------
def bool_equivalent(x):
return True if x else False
It's faster to write:
def bool_equivalent(x):
return not not x
Which Python used to do once upon a time -- and still does
in a way, because bool is a subclass of int.
The bool type was added mainly to provide a type that prints
out as 'True' or 'False' rather than 1 or 0. This can be
a considerable help for debugging and keeping the conceptual
meaning of one's data clear.
John said:Pascal got this right. (A nice feature of Pascal
was that "packed array of boolean" was a bit array).
C, which originally lacked a "bool" type, got it wrong.
So did Python.
Java is in the middle, with an isolated
"boolean" type but a system that allows casts.
Chris said:Remind me some day to finish work on my "ultimate programming
language", which starts out with a clean slate and lets the programmer
define his own operators and everything.
Unless you're sure all of a, b, c, and d are boolean values, an int
with a negative value slipping in could result in the sum equaling 1,
but more than one of the variables evaluating to True in boolean
contexts.
If they're all expressions, then you can easily guarantee that.
but I don't see how
(arbitrary expression) + (another expression) + ... + (last expression)
can have any guarantees applied. I mean, you can't even guarantee that
they won't raise an exception. Can you explain what you mean?
What Christian posted isn't something I've often done, but here's
something slightly different that exploits the same
comparisons-return-summable-values concept:
A condition with N subconditions is deemed to be satisfied if a
minimum of M of them are true. This is a general case of the boolean
Or (N = 2, M = 1) and And (N = 2, M = 2), but does not have a direct
equivalent in binary operators. You simply sum the subconditions,
compare against M, and you have your answer.
if (((port<1024) + (!ip.startswith("192.168.")) +
(greylist[ip]>time()) + (++spewcnt>10))>=3) // flag this packet as
suspicious
Contrived example as I don't recall any specifics right now, but this
will pick up any packets where three or more of the conditions are
met. Useful only in fairly specific situations, but I don't know of
any way to do this with just AND/OR/NOT that would be as clear and
simple.
Chris Angelico
Am 18.04.2011 21:58, schrieb John Nagle:
I find the behavior rather useful. It allows multi-xor tests like:
if a + b + c + d != 1:
?? ??raise ValueError("Exactly one of a, b, c or d must be true.")
I guess I never thought about it, but there isn't an 'xor' operator to
go along with 'or' and 'and'. Must not be something I need very often.
Jean-Paul Calderone said:Also boolean xor is the same as !=.
I guess I never thought about it, but there isn't an 'xor' operator to
go along with 'or' and 'and'. Must not be something I need very often.
Didn't someone already do that and call it "lisp"?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.