Usefulness of the "not in" operator

S

Steven D'Aprano

candide said:
Le 08/10/2011 14:01, Steven D'Aprano a écrit :


Obvious ? Not so. I performed some code mining and it appears that even
good sources make use of "not (foo in bar)" expressions.

All that proves is that even expert Python developers can, on occasion,
write non-idiomatic Python -- or that they accept code contributed by
non-expert Python developers, and don't bother adjusting trivial stylistic
flaws.

When I learned Pascal 20+ years ago, it took me a long time to stop
writing "x not in y" and learn the non-English-like "not x in y". Then I
learned Python, and it took me a while to stop writing Pascal code in
Python. Bad habits take a while to disappear.

(I never added superfluous semi-colons after each line though!)
 
R

Roy Smith

candide said:
After browsing source code, I realize that parenthesis are not necessary
("not" has higher precedence than "in").

Here's my take on parenthesis: If you need to look up whether they're
necessary or not, they are :)
 
C

Chris Angelico

                       ^^^^^^^^^^^^
????

I'm not sure what you're questioning, but it's possible to derive all
boolean operators from either nand or nor. Most hardware these days is
built out of silicon NAND gates, but there's no reason not to do it as
NOR gates.

ChrisA
 
R

Roy Smith

rusi said:
^^^^^^^^^^^^
????

Dualist (noun): a made up word referring to wrong-thinking people who
have rejected the teachings of the Prophet Nand and believe the nor is
the One True Operation on which all over operations can be constructed.
 
A

Alexander Kapps

Let's define the boolean values and operators using just two functions:

[SNIP]

Have you just explained Church booleans in an understandable
language? Awesome. I still have to chew on this, but I think this is
the first time where I might understand it. Thanks!

Even if it's off-topic, could you add some similar explanations for
Church numerals (maybe Lambda calculus it isn't too much?)
 
C

Chris Angelico

I sent this email twelve hours ago but to the wrong mailing list
*blush*. Since nobody else has raised the point, I'll repost it.

But both negations can be avoided by modus tollens.

"If you are able to start the car, the key is in the ignition."

But this translation implies looking at the result and ascertaining
the state, which is less appropriate to a programming language. It's
more like:

"If you found that you were able to start the car, the key must have
been in the ignition."

and is thus quite inappropriate to the imperative style. A functional
language MAY be able to use this style, but Python wants to have the
condition and then the action.

ChrisA
 
R

Roy Smith

Chris Angelico said:
I sent this email twelve hours ago but to the wrong mailing list
*blush*. Since nobody else has raised the point, I'll repost it.



But this translation implies looking at the result and ascertaining
the state, which is less appropriate to a programming language. It's
more like:

"If you found that you were able to start the car, the key must have
been in the ignition."

and is thus quite inappropriate to the imperative style. A functional
language MAY be able to use this style, but Python wants to have the
condition and then the action.

ChrisA

The key is in the ignition if you are able to start the car else you
hot-wired it.
 
A

Albert van der Horst

Oh, be consistent.

"If not the key is in the ignition, not you will be able to start the car."

But both negations can be avoided by modus tollens.

"If you are able to start the car, the key is in the ignition."

This is not normal speach. The connotation of an if sentence is
that the condition is something you have more control over
than over the result.

I sometime write
if 0 == i :
and get complaints, as if both sides of an identity are not
equivalent.
OTOH
if i == j :
and nobody cares it I wrote
if j == i :
And one could express "x not in s" as "(x in s) implies False" without
making the "not" explicit if "implies" was in the language. (I know
about <= but I also witnessed an unpleasant thread in another
newsgroup where people insisted that <= should not be defined for
truth values at all, and I also happen to like Python's "not in".)

Groetjes Albert
 
W

Westley Martínez

Here's my take on parenthesis: If you need to look up whether they're
necessary or not, they are :)

So we don't need precedence charts in the bathroom?
 
A

Alec Taylor

Let's define the boolean values and operators using just two functions:

def true(x, y):
   return x

def false(x, y):
   return y


That's all we need to define all of Boolean algebra. Unfortunately, it's a
bit ugly in Python:

<function true at 0xb7c3a36c>

So let's add a helper function to prettify the output:

def pr(b):
   print(b(true, false).__name__)

true

Much nicer!


Now define NAND:

def Nand(a, b):
   return (lambda c: lambda x, y: c(y, x))(a(b, a))


and we're done. All of boolean algebra can now be derived from Nand.


...     return Nand(b, b)
...
...     return Nand(Nand(a, b), Nand(a, b))
...
...     return Nand(Nand(a, a), Nand(b, b))
...
...     return And(Nand(a, b), Or(a, b))
...
false


and so forth.

Awesome
 
A

Alain Ketterlin

A

Alec Taylor

Unfortunately I don't know lambda [or for that matter, regular] calculus...

Alec Taylor said:
On Sun, Oct 9, 2011 at 3:08 AM, Steven D'Aprano
def true(x, y):
   return x

def false(x, y):
   return y [...]
def Nand(a, b):
   return (lambda c: lambda x, y: c(y, x))(a(b, a))

and we're done. [...]

Yes, that's how Church defined booleans in the lambda calculus. See
http://en.wikipedia.org/wiki/Church_encoding for encodings of natural
numbers and lists.

-- Alain.
 
C

candide

Le 10/10/2011 10:06, John Ladasky a écrit :
Yoda his name is. Programs in Forth he must.


;)


We can add to the list :

-- Tarzan
-- Geronimo
-- don Alexandro de la Vega dying in the arms of Zorro
....
 
N

Nobody

Even if it's off-topic, could you add some similar explanations for
Church numerals (maybe Lambda calculus it isn't too much?)

The Church numeral for N is a function of two arguments which applies its
first argument N times to its second, i.e. (f^N)(x) = f(f(...(f(x))...)).

IOW:

def zero(f, x):
return x

def one(f, x):
return f(x)

def two(f, x):
return f(f(x))

def three(f, x):
return f(f(f(x)))

And so on.

In general:

def applyN(n, f, x):
for i in xrange(n):
x = f(x)
return x

def church(n):
return lambda f, x: applyN(n, f, x)

seven = church(7) # this is the Church numeral for 7
seven(lambda x: x + 1, 0) 7
seven(lambda x: x * 2, 1) 128
seven(lambda x: x + ".", "")
'.......'
 
C

Chris Angelico

The Church numeral for N is a function of two arguments which applies its
first argument N times to its second, i.e. (f^N)(x) = f(f(...(f(x))...)).

Thanks - nice clear explanation. Appreciated. For an encore, can you
give an example of where this is actually useful? It seems a pretty
narrow utility.

ChrisA
 
I

Ian Kelly

Thanks - nice clear explanation. Appreciated. For an encore, can you
give an example of where this is actually useful? It seems a pretty
narrow utility.

It's useful for writing mathematical theorems about computability with
regard to the natural numbers using lambda calculus.
 
T

Terry Reedy

It's useful for writing mathematical theorems about computability with
regard to the natural numbers using lambda calculus.

Whereas pure set theorists define counts as sets so they can work with
counts within the context of pure set theory (in which everything is a
set). Other mathematicians use an axiomatic definition which pretty much
abstracts the common features of the set and function definitions.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top