Hi Danny!
Preliminary remark: I know that "beautiful" and "beauty" are concepts very seldom applied to computer programs or programming languages. I suppose mainly because they are to a large extent a matter of taste ...
Nevertheless: regardeless of the fact that I'm not a very skilled scheme-programmer, for me Scheme is the most beautiful programming language I know, mainly because of the clear and simple concepts which are purely realized. (I do *not* want to discuss here questions like functional versus object-oriented languages etc...)
However - Python - for me - is a programming language for the "pragmatic lover". Do you consider this expression as self explanatory? Lover -- because it makes so much fun to use it. Pragmatic -- because it is a language for the real world ;-)
im*h*o the primes - examples we are just discussing shows, that going *the Python way* may well be more successful or rewarding than trying to translate, traduce, ...er .. adapt Scheme-concepts quasi trying to show that Python is at least as good as Scheme. This is not the question. (I suppose that this also was not your goal?) A deeper question perhaps would be to what degree the content of cs texts depends on the choice of the mainly used language therein.
On the contrary - we may learn from this example that Python is a *very* good language in its own right - with carefully chosen concepts and syntax and a very slow, serious and publicly controlled development process resulting in a more and more easily and efficiently usable language.
Finally I'd like to stress that your contribution for me was very valuable and constructive - as are most of your innumerable contributions to the discussions on this list.
Thanks an regards, Gregor
post scriptum: as always when writing not only about technical facts but about opinions and values in English, I'm a bit axious if I was able to express correctly what I wanted to say. Hmmm. Hope that I didn't create any misunderstandings ....
Danny Yoo schrieb:
On Thu, 17 Mar 2005, Gregor Lingl wrote:
Hi! Who knows a more concise or equally concise but more efficient expression, which returns the same result as
[x for x in range(2,100) if not [y for y in range(2,x) if x%y==0]]
Hi Gregor,
Here is one that's traduced... er... adapted from material from the classic textbook "Structure and Interpretation of Computer Programs":
######
from itertools import ifilter, count
def notDivisibleBy(n):
... def f(x): ... return x % n != 0 ... return f ...
def sieve(iterable):
... nextPrime = iterable.next() ... yield nextPrime ... restOfPrimes = sieve(ifilter(notDivisibleBy(nextPrime), iterable)) ... for prime in restOfPrimes: ... yield prime ...
primes = sieve(count(2)) primes.next()
2
primes.next()
3
primes.next()
5
primes.next()
7
primes.next()
11
primes.next()
13
primes.next()
17
primes.next()
19
primes.next()
23 ######
The neat thing about this code is that it produces an infinite iterator of prime numbers. The original code in Scheme is itself quite concise and quite nice. It is described here:
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-24.html#call_footnote_Temp_455
Best of wishes to you!
-- Gregor Lingl Reisnerstrasse 3/19 A-1030 Wien
Telefon: +43 1 713 33 98 Mobil: +43 664 140 35 27
Autor von "Python für Kids" Website: python4kids.net _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor