Would Anonymous Functions Help in Learning Programming/Python?

3 views
Skip to first unread message

Cristian

unread,
Sep 21, 2007, 5:37:18 PM9/21/07
to
A friend of mine is an engineer and he's been needing to do more and
more programming as he goes on with is career. I convinced him to
learn Python instead of Perl and he's really started to like it. He
usually comes to me when he can't accomplish a task with his current
knowledge and I introduce him to a new feature in Python. FYIW, he
thought List Comprehensions were freakin' awesome. He's started
writing his own personal scripts for tasks like web scraping. So, from
personal experience, Python truly is a great first language to learn.

Although his learning experience has gone mostly smoothly, he's hit a
lot of speed bumps with functions. Specifically, he's having trouble
thinking of functions as first order data (don't worry, I haven't
confused him with such terminology yet). He had a little trouble
understanding that you can pass functions as arguments to other
functions (e.g., passing a key to the list.sort method). He also had a
little trouble grasping functions within other functions. Last but not
least, he had trouble grasping methods in class declarations,
especially the required self as the first argument (I'm sure he wasn't
the first).

Now, my friend's a smart guy so I know it isn't any lack of brain
cells on his part. I still remember many students in my CS classes
having trouble grasping the very same concept. And, after we finally
get a hold of first order functions, we appreciate its incorporation
into languages. It would be a shame if my friend just learns the
motions and never incorporates first order functions into his
programs. I began to wonder if there was anything Python could do to
help newcomers grasp the power of first order functions or, as
Pythonistas put it, everything is an object.

To me, the biggest setback for new programmers is the different syntax
Python has for creating functions. Instead of the common (and easy to
grasp) syntax of foo = bar Python has the def foo(): syntax. So, when
a new programmer is first introduced to functions they are immediately
confronted with the notion that functions are "different". After all,
they have their own special syntax. This seems to only further the
separation newbies make between "data" and "functions" or "stuff" and
"actions". Now, the vast majority of us learned this dichotomy when we
first began to program, so we are ingrained to assume and even expect
a different syntax for function declaration, but in a program like
Python there doesn't seem to be any other reason to have it.
Furthermore, I think it actually inhibits the learning of the
uninitiated. We can, of course, keep the current syntax as sugar.

To someone who's learning to program wouldn't a syntax like the
further give them all they need and also reinforces the idea that
functions are data just like everything else?

my_function = function(foo, bar): pass
an_instance_method = function(self, foo): pass
a_method_declaration = method(self, foo): pass

The last one is mostly my pet peeve of having Python "magically"
create methods out of (what is essentially) a function declaration.
When I first learned it, it felt wrong but you had to press through it
because there was really no other way of declaring methods.

What do you think? Have you hit this roadblock when helping others
learn Python? Does the current syntax make you feel that functions are
still treated as second class (get it?) citizens?

chris.m...@gmail.com

unread,
Sep 21, 2007, 5:48:02 PM9/21/07
to

There are already anonymous functions in Python.

lambda x, y, z: x + y + z

is the same as:

def _(x, y, z): return x + y + z

As for the method stuff, check out staticmethod(). If you assign
staticmethod(<function here>) to an object, it will be treated as a
normal function and not as a "method."

I have my own personal opinions about how methods should be in Python,
but, whatever. It's weird to deal with stuff like this:

x.y = re.match # Assign a function to an attribute of a class, but it
doesn't work because you can't assign anything but methods!
x.y = staticmethod(re.match) # Ugly

Cristian

unread,
Sep 21, 2007, 6:07:55 PM9/21/07
to
On Sep 21, 2:48 pm, chris.monsa...@gmail.com wrote:
> There are already anonymous functions in Python.
>
> lambda x, y, z: x + y + z
>
> is the same as:
>
> def _(x, y, z): return x + y + z
>
> As for the method stuff, check out staticmethod(). If you assign
> staticmethod(<function here>) to an object, it will be treated as a
> normal function and not as a "method."
>
> I have my own personal opinions about how methods should be in Python,
> but, whatever. It's weird to deal with stuff like this:
>
> x.y = re.match # Assign a function to an attribute of a class, but it
> doesn't work because you can't assign anything but methods!
> x.y = staticmethod(re.match) # Ugly

True, there is lambda, but that is very limited. It might be useful
for key arguments, but not much else. It doesn't solve the teaching
problem of "See, functions are just like any other data type. You can
assign it to a variable." It would be a footnote if it's mentioned at
all. My hope is to subtly reinforce the notion that functions are data
and can be passed around. The current function declaration doesn't
help with this. Creating a function and assigning it to a name is
exactly what Python does, why not have it come out in the syntax? It's
not necessary, yes, but I think it would be helpful for teaching
purposes.

Again, it's not necessary as much as it's more intuitive and obvious
what's going on. This helps a beginner sort out the process from the
syntax without taking it on faith. They can see the class declaration
and see "I'm defining just another attribute to this class only this
time it happens to be method".

There is nothing functionally lacking in Python. I'm just curious if
there's anything Python can do syntax-wise to help a person better
grasp programming ideas and Python's inner workings.

chris.m...@gmail.com

unread,
Sep 21, 2007, 6:22:02 PM9/21/07
to

Guido apparently doesn't like lambda; I'm not really sure why, it's
extremely useful. There were rumors of it leaving in Python 3000, but
thankfully there was the decision to keep them. (I have a feeling if
they weren't kept in, a project fork would have happened or such.)
Anyway, one of the biggest problems implementation wise is indentation
in an expression - there is no expression currently that uses
significant whitespace. Python draws a pretty clear line between
expression and statement. I do agree with you however, it seems as if
there is an arbitrary line between function definitions and normal
variable assignment that shouldn't be there for the sake of
consistency.

A question: if you WERE to implement function definitions as normal
expressions, how would you go about embedding it within an expression?

x = map(def a:
<line of code>
<line of code>
<line of code>
, [1, 2, 3])

It looks hideous in my opinion and lining up the , with the def is
ugly. Not to mention currently, inside groupings, whitespace is
ignored. How would you handle a whitespace signif. expression inside a
grouping which by definition ignores whitespace?

Cristian

unread,
Sep 21, 2007, 6:36:32 PM9/21/07
to

Yeah, I agree, that does look pretty ugly. Correct me if I'm wrong,
but I thought the way Python determines a block is by the whitespace
of the first line. So, as long as the spacing (or !tabbing!) is
consistent from line to line the parser will know it's part of the
same block. From that I don't think the parser would have much trouble
extracting the function definition from the above example. I would
change the word "def". That's never been informative enough for me. I
would follow Javascript and call it "function." Also, I think
parentheses should be required.

If there's no implementation problem, I don't see why Python shouldn't
allow the above example, but I would certainly discourage it. Just
like Python doesn't prevent you from writing an insanely long and
convoluted function, Python shouldn't enforce any arbitrary length in
anonymous functions. I think the above example should be discouraged
as a style violation, not syntax.

Ron Adam

unread,
Sep 21, 2007, 6:44:39 PM9/21/07
to Cristian, pytho...@python.org

Cristian wrote:
> My hope is to subtly reinforce the notion that functions are data
> and can be passed around. The current function declaration doesn't
> help with this. Creating a function and assigning it to a name is
> exactly what Python does, why not have it come out in the syntax? It's
> not necessary, yes, but I think it would be helpful for teaching
> purposes.

I think key may be to discuss names and name binding with your friend. How
a name is not the object it self, like a variable is in other languages.
For example show him how an object can have more than one name. And discus
how names can be bound to nearly anything, including classes and functions.


> Again, it's not necessary as much as it's more intuitive and obvious
> what's going on. This helps a beginner sort out the process from the
> syntax without taking it on faith. They can see the class declaration
> and see "I'm defining just another attribute to this class only this
> time it happens to be method".
>
> There is nothing functionally lacking in Python. I'm just curious if
> there's anything Python can do syntax-wise to help a person better
> grasp programming ideas and Python's inner workings.

You could also discus factory functions with him. Once he gets that a
function can return another function, then it won't be so much of a leap
for a function to take a function as an argument.

Of course he'll figure out all this sooner or later anyway. You can't be
an engineer without a fair amount of brain cells committed to processing
abstract concepts.

Cheers,
Ron

Ron Adam

unread,
Sep 21, 2007, 6:44:39 PM9/21/07
to pytho...@python.org, pytho...@python.org

Cristian wrote:
> My hope is to subtly reinforce the notion that functions are data
> and can be passed around. The current function declaration doesn't
> help with this. Creating a function and assigning it to a name is
> exactly what Python does, why not have it come out in the syntax? It's
> not necessary, yes, but I think it would be helpful for teaching
> purposes.

I think key may be to discuss names and name binding with your friend. How

a name is not the object it self, like a variable is in other languages.
For example show him how an object can have more than one name. And discus
how names can be bound to nearly anything, including classes and functions.

> Again, it's not necessary as much as it's more intuitive and obvious
> what's going on. This helps a beginner sort out the process from the
> syntax without taking it on faith. They can see the class declaration
> and see "I'm defining just another attribute to this class only this
> time it happens to be method".
>
> There is nothing functionally lacking in Python. I'm just curious if
> there's anything Python can do syntax-wise to help a person better
> grasp programming ideas and Python's inner workings.

You could also discus factory functions with him. Once he gets that a

Cristian

unread,
Sep 21, 2007, 7:02:21 PM9/21/07
to
On Sep 21, 3:44 pm, Ron Adam <r...@ronadam.com> wrote:

> I think key may be to discuss names and name binding with your friend. How
> a name is not the object it self, like a variable is in other languages.
> For example show him how an object can have more than one name. And discus
> how names can be bound to nearly anything, including classes and functions.

I could discuss name binding but it would be great if Python said this
itself. After all, you can even bind a module with the foo = bar
syntax by using __import__ function. If function definitions followed
the same pattern, I think a beginner would subconsciously (maybe even
consciously) realize that function names are just like everything
else. Actually, this would be helpful for many people. If you come
from a language like Java you're used to thinking of attributes and
methods as living in different namespaces. I think a new syntax will
encourage seasoned programmers think in a more Pythonic way.

Python has done a very good job in easing people into programming. My
friend doesn't come to me very often because the syntax is clear and
simple and the builtin datatypes allow you to do so much. My goal is
that I would never have to explain to him about name binding; that
he'd pick it up by learning the language on his own. He's learned
lists, dictionaries and even some OOP without me. I don't think name
binding would be a stretch.

> You could also discus factory functions with him. Once he gets that a
> function can return another function, then it won't be so much of a leap
> for a function to take a function as an argument.

I think this isn't the most intuitive way of approaching first order
functions. It's true that if a function can return another function
then a function must be first order (i.e., it's just like any other
variable), but that seems almost backwards to me. I think it would
make more sense to have beginners _know_ that functions are like all
other variables and can therefore be passed by other functions or
returned by other functions. That I think would be better accomplished
if they define functions the same way you would define other variables
that you know can be passed and returned.


Donn Cave

unread,
Sep 21, 2007, 7:18:15 PM9/21/07
to
In article <1190410638.2...@y42g2000hsy.googlegroups.com>,
Cristian <super.sg...@gmail.com> wrote:
...

> To someone who's learning to program wouldn't a syntax like the
> further give them all they need and also reinforces the idea that
> functions are data just like everything else?
>
> my_function = function(foo, bar): pass
> an_instance_method = function(self, foo): pass
> a_method_declaration = method(self, foo): pass

I think one followup has already alluded to the division
between Python `statements' and `expressions'. The `def'
statement may create and bind a value, but since it's a
statement and not an expression, it doesn't have any value.
Python is not a functional programming language. It probably
could be reinvented to eliminate statements, but ... there
are already plenty of languages to choose from.

If we're going there, though, I think it's obvious that
once you have defined

an_instance_method = function(self, foo): ...

it should be invoked

an_instance_method(an_instance, foo)

which would be much less messy in nested expressions where
the current standard OOP notation flips from right to left
too much ...

string.join(x.split('-'), '').lower()

--> lower(string.join('', split(x, '-')))

It might make for some interesting problems, but that's what
it's about, changing things so it stays fun for everyone.
At the same time, partial function parameter binding should
be implemented, so you could say

dotted = string.join('.')
...
v = dotted(['comp', 'lang', 'python'])

As you probably well know, that isn't my idea, it's a common
functional programming language idiom.

Donn Cave, do...@u.washington.edu

J. Cliff Dyer

unread,
Sep 21, 2007, 7:27:30 PM9/21/07
to python help
Cristian wrote:
> On Sep 21, 3:44 pm, Ron Adam <r...@ronadam.com> wrote:
>
>
>> I think key may be to discuss names and name binding with your friend. How
>> a name is not the object it self, like a variable is in other languages.
>> For example show him how an object can have more than one name. And discus
>> how names can be bound to nearly anything, including classes and functions.
>>
>
> I could discuss name binding but it would be great if Python said this
> itself. After all, you can even bind a module with the foo = bar
> syntax by using __import__ function. If function definitions followed
> the same pattern, I think a beginner would subconsciously (maybe even
> consciously) realize that function names are just like everything
> else. Actually, this would be helpful for many people. If you come
> from a language like Java you're used to thinking of attributes and
> methods as living in different namespaces. I think a new syntax will
> encourage seasoned programmers think in a more Pythonic way.
>

However, you still have to solve the problem of using a single-line
construct (x = y) with a multi-line definition. That is the essential
difference that def is designed to solve. The __import__ trick works
because import is also a single line construct.

The only proposal given in this thread is using consistent indentation
within the parentheses, but parentheses are already explicitly designed
to let you ignore the whitespace rules. To suddenly create a situation
in which you have significant whitespace on the right side of an
assignment statement, and *within parentheses* will break too much code,
and make the solution unnecessarily ugly. Multi-line lambdas have been
rejected because of this very problem, so unless you have a clean
solution, I think your proposal falls into the category of "would be
nice, but not in Python."

Cheers,
Cliff

Sean Tierney

unread,
Sep 21, 2007, 7:47:17 PM9/21/07
to Cristian, pytho...@python.org
Just tell him that "functions are like all other variables and can

therefore be passed by other functions or returned by other functions.
"

If your friend understands variables and functions and he can't make
the "leap" (and assuming you're right, of course) then your friend
doesn't understand variables and functions.

Happy Friday.

Sean

> --
> http://mail.python.org/mailman/listinfo/python-list
>


--
Sean Tierney

Sean Tierney

unread,
Sep 21, 2007, 7:53:41 PM9/21/07
to Cristian, pytho...@python.org
On 9/21/07, Sean Tierney <seantt...@gmail.com> wrote:
> Just tell him that "functions are like all other variables and can

> therefore be passed by other functions or returned by other functions.
> "
>
> If your friend understands variables and functions and he can't make
> the "leap" (and assuming you're right, of course) then your friend
> doesn't [might not] understand variables and functions [and he might need patience b/c this stuff is hard and not everyone is as smart as everyone else].
>
> Happy Friday.
>
> Sean [moderately overweight, self-admitted (very) slow learner.]

>
> On 9/21/07, Cristian <super.sg...@gmail.com> wrote:
> > --
> > http://mail.python.org/mailman/listinfo/python-list
> >
>
>
> --
> Sean Tierney
>


--
Sean Tierney

Cristian

unread,
Sep 21, 2007, 8:00:03 PM9/21/07
to

http://www.artima.com/weblogs/viewpost.jsp?thread=147358

You, Guido, and I all agree that anonymous functions in expressions
are ugly. There's no argument there. I wouldn't expect any self
respecting programmer to do such a thing even if it was available to
them (in Python that is).

I suppose my question is, taking into account the potential of ugly
code that could be created, and the fact that it's technically
feasible, would it still be worth adding anonymous functions to
explicitly show the first order nature of functions and show that
functions are in the same namespace as all other variables?

I suppose a solution could be to allow the my_function = function(foo,
bar): ... syntax but throw a syntax error if the function isn't being
bound to a variable, but that would cause other problems. From that
syntax you would assume that it's possible to create an anonymous
function wherever a variable is expected. That's not very intuitive
either.

Cristian

unread,
Sep 21, 2007, 8:08:40 PM9/21/07
to
On Sep 21, 4:47 pm, "Sean Tierney" <seanttier...@gmail.com> wrote:
> Just tell him that "functions are like all other variables and can
> therefore be passed by other functions or returned by other functions.
> "
>

I could """Just tell him that "functions are like all other variables


and can
therefore be passed by other functions or returned by other functions.

" """ but wouldn't it be great if Python did this for me? It seems to
me that the ability for Python to explicitly state that functions-are-
like-other-variables is there so why not expose it?

> If your friend understands variables and functions and he can't make
> the "leap" (and assuming you're right, of course) then your friend
> doesn't understand variables and functions.

To say that you understand variables and functions is language
specific. You can't translate your knowledge of variables and
functions from Python to Java and vice-versa. They are treated
completely different. Perhaps you can translate your Python
understanding of functions and variables to Javascript but not to C.


Carl Banks

unread,
Sep 21, 2007, 7:52:41 PM9/21/07
to
On Fri, 21 Sep 2007 21:37:18 +0000, Cristian wrote:
> Although his learning experience has gone mostly smoothly, he's hit a
> lot of speed bumps with functions. Specifically, he's having trouble
> thinking of functions as first order data (don't worry, I haven't
> confused him with such terminology yet). He had a little trouble
> understanding that you can pass functions as arguments to other
> functions (e.g., passing a key to the list.sort method). He also had a
> little trouble grasping functions within other functions. Last but not
> least, he had trouble grasping methods in class declarations, especially
> the required self as the first argument (I'm sure he wasn't the first).

First of all, let me say that I think "functions as first class data" is
helpful, but not crucial, to programming in Python, and there are many
people who simply don't need the lesson. Especially someone like an
engineer (in the classical sense), who isn't building versatile software
packages, won't need to resort to functional programming much. For
straightforward tasks, like sorting lists with a custom ordering,
functional progamming merely a convenience, and can be done without.

So I'm -1 on syntactic changes to the language to support a pedagogical
use that's not crucially important.


Now, as for the more general question--How do you teach functional
concepts?--because some people do need to know it. I suspect it's one of
those things where there are two possibilities and not much in between.
Either

1. The person gets it right away.

or

2. The person doesn't get it right away.

In the latter case, trying to teach it conceptually is probably
hopeless. Best thing to do is teach functional usage for certain use
cases. For instance, teach someone to define a function returning a key,
and have them use "key=myfunc" in list.sort--and not worry about why it
works. If they keep at it, eventually one of two things will happen.
Either,

2a. The person will have an "A ha! I get it now!" moment, and will
finally understand the concept.

or

2b. The person will never get it.


Carl Banks

Ron Adam

unread,
Sep 21, 2007, 8:33:16 PM9/21/07
to pytho...@python.org

Cristian wrote:
> On Sep 21, 3:44 pm, Ron Adam <r...@ronadam.com> wrote:
>
>> I think key may be to discuss names and name binding with your friend. How
>> a name is not the object it self, like a variable is in other languages.
>> For example show him how an object can have more than one name. And discus
>> how names can be bound to nearly anything, including classes and functions.
>
> I could discuss name binding but it would be great if Python said this
> itself. After all, you can even bind a module with the foo = bar
> syntax by using __import__ function. If function definitions followed
> the same pattern, I think a beginner would subconsciously (maybe even
> consciously) realize that function names are just like everything
> else. Actually, this would be helpful for many people. If you come
> from a language like Java you're used to thinking of attributes and
> methods as living in different namespaces. I think a new syntax will
> encourage seasoned programmers think in a more Pythonic way.

I could see methods having their own keywords. Then functions defined in
classes would be static methods without any extra work. But to do that
would break a lot of already existing code as well as confuse a lot of
current users.


> Python has done a very good job in easing people into programming. My
> friend doesn't come to me very often because the syntax is clear and
> simple and the builtin datatypes allow you to do so much. My goal is
> that I would never have to explain to him about name binding; that
> he'd pick it up by learning the language on his own. He's learned
> lists, dictionaries and even some OOP without me. I don't think name
> binding would be a stretch.

Chances are he'll run into a gotcha where an object has two names and sort
it out that way. Which is why I suggested starting there. It will save
him some grief if he hasn't run into it yet.


>> You could also discus factory functions with him. Once he gets that a
>> function can return another function, then it won't be so much of a leap
>> for a function to take a function as an argument.
>
> I think this isn't the most intuitive way of approaching first order
> functions.

The Python tutorial does this by defining a function, then assigning it to
another name and calling it from the new name.

http://www.python.org/doc/current/tut/tut.html


> It's true that if a function can return another function
> then a function must be first order (i.e., it's just like any other
> variable), but that seems almost backwards to me. I think it would
> make more sense to have beginners _know_ that functions are like all
> other variables and can therefore be passed by other functions or
> returned by other functions. That I think would be better accomplished
> if they define functions the same way you would define other variables
> that you know can be passed and returned.

I think it gets awkward fairly quick if you try and work out how to do
this. There have been requests in the past to have functions assigned to
names like other things.

The nice thing about the current syntax is it more closely resembles what
you would type at call time, so it is more self documenting.

def sum(x, y):
return x + Y

total = sum(1, 2)


I think changing that would be trading one type of clarity for another.


Ron


Steven D'Aprano

unread,
Sep 21, 2007, 9:17:48 PM9/21/07
to
On Fri, 21 Sep 2007 22:07:55 +0000, Cristian wrote:

> True, there is lambda, but that is very limited. It might be useful for
> key arguments, but not much else.

No, lambda is useful for anything that any other function is useful for,
provided that you can write it as a single expression and not need to use
statements. In other words, a lambda function can do anything anything
any other function can, just less conveniently.

(To pick one example at random, you can use print with lambda:
http://northernplanets.blogspot.com/2006/07/python-using-print-with-lambda.html
and while it is easy it isn't exactly convenient.)

And that's basically why function definitions have the syntax they do,
because trying to hammer a multi-line function definition into a single
expression is painful.


> It doesn't solve the teaching problem
> of "See, functions are just like any other data type. You can assign it
> to a variable."


Is their interactive interpreter broken?


>>> def parrot(colour='red'):
... return "I'm a parrot with %s plumage." % colour
...
>>> bird = parrot # note the lack of brackets
>>> type(bird)
<type 'function'>
>>> bird('green')
"I'm a parrot with green plumage."


Or using lambda:


>>> parrot = lambda colour: "I'm a parrot with %s plumage." % colour
>>> parrot("purple")
"I'm a parrot with purple plumage."

> It would be a footnote if it's mentioned at all. My hope
> is to subtly reinforce the notion that functions are data and can be
> passed around. The current function declaration doesn't help with this.

Some things just need to be learnt. I'm in favour of making languages
easy for newbies to learn, but making function and method definitions
harder to use just so newbies will be given a subtle reminder of
something that 80% of them will never notice or use anyway is a bad
trade-off.


> Creating a function and assigning it to a name is exactly what Python
> does, why not have it come out in the syntax? It's not necessary, yes,
> but I think it would be helpful for teaching purposes.

If people don't get it when you EXPLICITLY show them that functions are
first-class objects, how do you expect them to notice it on their own
based on the IMPLICIT similarities in syntax?


--
Steven.

Scott David Daniels

unread,
Sep 21, 2007, 10:17:36 PM9/21/07
to
Cristian wrote:
> On Sep 21, 3:44 pm, Ron Adam <r...@ronadam.com> wrote:
>
>> I think key may be to discuss names and name binding with your friend.

Here's an idea:

import math

def sin_integral(start, finish, dx):
total = 0.0
y0 = math.sin(start)
for n in range(1, 1 + int((finish - start) / float(dx))):
y1 = math.sin(start + n * dx)
total += (y0 + y1)
y0 = y1
return total / 2. * dx


def cos_integral(start, finish, dx):
total = 0.0
y0 = math.sin(start)
for n in range(1, 1 + int((finish - start) / float(dx))):
y1 = math.cos(start + n * dx)
total += (y0 + y1)
y0 = y1
return total / 2. * dx

generalize and separate the integration technique from the
function it integrates.

Marc 'BlackJack' Rintsch

unread,
Sep 22, 2007, 2:26:39 AM9/22/07
to
On Fri, 21 Sep 2007 19:52:41 -0400, Carl Banks wrote:

> First of all, let me say that I think "functions as first class data" is
> helpful, but not crucial, to programming in Python, and there are many
> people who simply don't need the lesson. Especially someone like an
> engineer (in the classical sense), who isn't building versatile software
> packages, won't need to resort to functional programming much.

Of course you don't *need* functional programming as in "there's no way
around it", but why are engineers such an exception!? I find the stuff in
`itertools` very handy when it comes to filter, manipulate, group and
aggregate data from large log files in ad hoc scripts. I'm not an
engineer but I guess there are similar tasks with log files or measurement
results.

> For straightforward tasks, like sorting lists with a custom ordering,
> functional progamming merely a convenience, and can be done without.

``for`` loops are just a convenience, you can do without. ;-)

Ciao,
Marc 'BlackJack' Rintsch

Paul Rubin

unread,
Sep 22, 2007, 2:38:38 AM9/22/07
to
Carl Banks <pavlove...@gmail.com> writes:
> Especially someone like an
> engineer (in the classical sense), who isn't building versatile software
> packages, won't need to resort to functional programming much.

http://www.math.chalmers.se/~rjmh/Papers/whyfp.html

Kay Schluehr

unread,
Sep 22, 2007, 2:49:08 AM9/22/07
to
On 22 Sep., 00:36, Cristian <super.sgt.pep...@gmail.com> wrote:

> Yeah, I agree, that does look pretty ugly. Correct me if I'm wrong,
> but I thought the way Python determines a block is by the whitespace
> of the first line. So, as long as the spacing (or !tabbing!) is
> consistent from line to line the parser will know it's part of the
> same block. From that I don't think the parser would have much trouble
> extracting the function definition from the above example. I would
> change the word "def". That's never been informative enough for me.

Here is the grammar:

http://svn.python.org/projects/stackless/tags/python-2.5/Grammar/Grammar

If you feel you can transform it into another unambigous grammar
mixing statements and expressions it's up to you. Guido at least does
not seem to show interest:

http://www.artima.com/weblogs/viewpost.jsp?thread=147358

When you are working on it you'll need a parser generator that also
checks your changes.
EasyExtend might help

http://www.fiber-space.de/EasyExtend/doc/EE.html

You can use it with Python 2.5, create a new fiber and a Grammar.ext
file. Note that the parser generator is LL(1) so it is not all
powerfull but *very* efficient. Tokenization is performed separately.
INDENT, DEDENT are indentation token used within the definition of the
suite nonterminal. I'd provide additional help when you get stuck.

Finally the standard response to your claims: "this is all open
source". This advice might be annoying and uncomfortable and maybe you
just want to talk about some problems and make a few guesses instead
of solving them actually. We are all Web 2.0 now and discussing issues
and doing socialization might be more important than everything else
even among geeks. This can be confusing however for people who believe
that softskills are not everything.

Regards, Kay

Paul Rubin

unread,
Sep 22, 2007, 2:56:58 AM9/22/07
to
Kay Schluehr <kay.sc...@gmx.net> writes:
> If you feel you can transform it into another unambigous grammar
> mixing statements and expressions it's up to you.

We got rid of the print statement for python 3.0. Why not get rid
of the rest of them too? Just use expressions for everything, as
works fine for plenty of other languages.

Ron Adam

unread,
Sep 22, 2007, 3:02:57 AM9/22/07
to Scott David Daniels, pytho...@python.org


How about this?

It's based on the apple basic program example in How to Enjoy Calculus.


Ron


import math

def integrate(fn, x1, x2, n=100):
# Calculate area of fn using Simpson's rule.
width = float(x2 - x1) / n
area = fn(x1)
if n % 2 != 0: # make sure its even
n += 1
for n in range(1, n):
x = x1 + n * width
if n % 2 == 0:
area += 2.0 * fn(x)
else:
area += 4.0 * fn(x)
area += fn(x2)
return area * (width / 3.0)


def fn(x):
return x**2

print "Area of fn:", integrate(fn, 0, 2)

print "Area of cos fn:", integrate(math.cos, 1, 2)

print "Area of sin fn:", integrate(math.sin, 1, 2)


Area of fn: 2.66666666667
Area of cos fn: 0.0678264420216
Area of sin fn: 0.956449142468

Ron Adam

unread,
Sep 22, 2007, 3:02:57 AM9/22/07
to pytho...@python.org, pytho...@python.org

Kay Schluehr

unread,
Sep 22, 2007, 3:47:37 AM9/22/07
to
On 22 Sep., 08:56, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:

One might create a new dynamic functional+OO programming language,
where small statements like print, assert, raise etc. all become
functions and statements like while, for, with etc. become anonymous
closures. I think Logix had gone once in this direction but was
abandoned due to severe technical problems. So one might eventually
revive this project with another more sound technical base. Personally
I'm not sure it's worth a large effort and whether EasyExtend is the
right toolkit at hand. So it's definitely not Guido who will revive it
and also not me. Maybe some PyPy guys? What are we talking about -
really?

Marc 'BlackJack' Rintsch

unread,
Sep 22, 2007, 4:40:53 AM9/22/07
to
On Sat, 22 Sep 2007 00:47:37 -0700, Kay Schluehr wrote:

> On 22 Sep., 08:56, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>> Kay Schluehr <kay.schlu...@gmx.net> writes:
>> > If you feel you can transform it into another unambigous grammar
>> > mixing statements and expressions it's up to you.
>>
>> We got rid of the print statement for python 3.0. Why not get rid
>> of the rest of them too? Just use expressions for everything, as
>> works fine for plenty of other languages.
>
> One might create a new dynamic functional+OO programming language,
> where small statements like print, assert, raise etc. all become
> functions and statements like while, for, with etc. become anonymous
> closures.

Before someone starts to create such a thing he should take a look at Io
which has just objects and methods.

http://www.iolanguage.com/

Ciao,
Marc 'BlackJack' Rintsch

Carl Banks

unread,
Sep 22, 2007, 4:51:31 AM9/22/07
to

From the link:

"Abstract
As software..."

That's it, lost me already. You ever see the kinds of programs
mechanical engineers write? It isn't software.


Carl Banks

Paul Rubin

unread,
Sep 22, 2007, 5:32:30 AM9/22/07
to
Carl Banks <pavlove...@gmail.com> writes:
> That's it, lost me already. You ever see the kinds of programs
> mechanical engineers write? It isn't software.

They do a lot of number crunching. Certainly they can appreciate
higher order functions like integrate(f, x0, x1) to integrate the
function f from x0 to x1; or diff(f, x0) to find the derivative of f
at x0; etc. Fortran had cheesy ways to do this as far back as the
1950's.

Kay Schluehr

unread,
Sep 22, 2007, 5:44:35 AM9/22/07
to

I checked out Io once and I disliked it. I expected Io's prototype OO
being just a more flexible variant of class based OO but Io couples a
prototype very closely to its offspring. When A produces B and A.f is
modified after production of B also B.f is modified. A controls the
state of B during the whole lifetime of B. I think parents shall not
do this, not in real life and also not in programming language
semantics.

There was another, similar and also abandoned project a while ago
heading for prototype OO called Prothon:

http://mail.python.org/pipermail/python-announce-list/2004-March/002966.html

When I remember correctly it was killed not because it was too
ambitious but the author lost his vision and didn't want to grow
Prothons ecosystem.


Marc 'BlackJack' Rintsch

unread,
Sep 22, 2007, 7:15:53 AM9/22/07
to
On Sat, 22 Sep 2007 02:44:35 -0700, Kay Schluehr wrote:

> I checked out Io once and I disliked it. I expected Io's prototype OO
> being just a more flexible variant of class based OO but Io couples a
> prototype very closely to its offspring. When A produces B and A.f is
> modified after production of B also B.f is modified. A controls the
> state of B during the whole lifetime of B. I think parents shall not
> do this, not in real life and also not in programming language
> semantics.

Well it's like Python: inherited slots (attributes) are looked up in the
ancestors. It should be easy to override `Object clone` in Io, so all
slots of the ancestor are shallow copied to the clone, but I guess this
might break existing code. At least for your own code you could introduce
a `realClone` slot.

Ciao,
Marc 'BlackJack' Rintsch

Carl Banks

unread,
Sep 22, 2007, 7:04:46 AM9/22/07
to

Well, I appreaciate it, as do many other engineers, but for a lot of
engineers functional programming might as well not exist, either because
they don't about it or don't care about it. And it doesn't really affect
them much, other than making them slightly less efficient then they could
have been. That's the key here.


Carl Banks