# [Edu-sig] Kirby Urner's Sieve of Eratosthenes

Dustin James Mitchell djmitche@midway.uchicago.edu
Mon, 5 Jun 2000 11:58:16 -0500 (CDT)

```On Mon, 5 Jun 2000, John Posner wrote:

> In my opinion, Kirby's implementation (see below) works too hard. The
> critical problem is that it uses an old-fashioned LOOP to do the work
> instead of using a nifty, Pythonic FILTER. After all, a SIEVE is just a kind
> of FILTER.

> NOTE 2: My implementation is a main program, not a function. Packaging it up
> as a function (say, "erat") introduces a scoping problem: when you attempt
> to define the lambda function within the "erat" function, it can't see the
> "erat" function's local variable "prm". Solution: use a default argument to
> implement a closure, passing the "erat" function's local variable into the
> scope of the labmda function (cf. "Programming Python", Mark Lutz, O'Reilly
> & Associates, p. 746):

While correct, and better in the eyes of a Python pro, I think that your
modifications stray too far from Kirby's "math through programming" bent
-- there is no clear mathematical analogue to a filter, and it further
introduces the scoping problem you mention.

Which brings me to an interesting point -- Python is great for many kinds
of math/programming lessons, but it has one critical flaw in its
implementation of the lambda operator, as you mention above.  While
students can still do interesting experiments with functions a la

>>> def twice(f, x):
return f(f(x))

>>> twice(lambda x : x + x, 4)
16
...

you run into a frustrating brick wall when you try to include local
variables in the lambda.  Although you can use the default-argument trick,
it's difficult to explain that to students who have not yet mastered
Python.  The answer is `it's that way because I said so', and (IMHO)
that's not the way to design a curriculum.

My \$0.02

Dustin

---------------------------------------------------------------------
|                         Dustin Mitchell                )O(        |
---------------------------------------------------------------------

```