[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Accumulator
Paul Prescod <paul@prescod.net> writes:
[...]
> > Actually, I was surprised how badly Python did. I had till
> > then believed the story that Python was equivalent to Lisp
> > except for macros.
>
> I don't think Guido ever intended that! Python programmers should do
> things in the Python way. State + Behaviour = Instance, not closure.
:-(
i've also read a thread explaining that because of list comprehension,
map/reduce/filter were more or less deprecated.
i've yet to see examples where functional programming can be done the
OO way. IMO this is /somewhat/ orthogonal. And list comprehension are
another way of writing *some* functional programming, but are really
limited to lists whereas functional programming is not! (think
variants)
> > .... I was kind of shocked, for example, to
> > find that you can't put the same things in the body of a
> > lambda expression as you can in a named function. That
> > just seems like a bug to me.
>
> You're right, it is a bug. But you're missing something. Adding lambda
> to an OO language was a bug to start with. Guido hasn't "fixed" lambda
> because he wants it to go away. Trying to be Lisp was the mistake.
>
> "Q. What feature of Python are you least pleased with?
>
> Guido: Sometimes I've been too quick in accepting contributions, and
> later realized that it was a mistake. One example would be some of the
> functional programming features, such as lambda functions. lambda is a
> keyword that lets you create a small anonymous function; built-in
> functions such as map, filter, and reduce run a function over a sequence
> type, such as a list."
I had not read this the way you did. The following that comes with
Guido's answer:
# In practice, it didn't turn out that well. Python only has two
# scopes: local and global. This makes writing lambda functions painful,
# because you often want to access variables in the scope where the
# lambda was defined, but you can't because of the two scopes. There's a
# way around this, but it's something of a kludge. Often it seems much
# easier in Python to just use a for loop instead of messing around with
# lambda functions. map and friends work well only when there's already
# a built-in function that does what you want.
I understand that having lambda with no closure was the error, which
is now fixed in python 2.2
[...]
> > > Also, if anyone has time, I would be curious what the Common Lisp
> > > equivalent of the class-based version looks like.
> > >
> >
> > God only knows.
>
> Are you saying it is hard to translate this into Common Lisp?
>
> class acc:
[...]
(defclass Acc () ((cur :accessor cur :initarg :start)))
(defmethod inc ((self Acc) howmuch) (setf (cur self) (+ (cur self) howmuch)))
(defmethod dec ((self Acc) howmuch) (setf (cur self) (- (cur self) howmuch)))
(setq a (make-instance 'Acc :start 20))
(inc a 5)
(dec a 3)
(cur a)
?