merits of Lisp vs Python
André Thieme
address.good.until.2006.dec.22 at justmail.de
Mon Dec 11 09:10:33 EST 2006
Bill Atkins schrieb:
> Bill Atkins <atkinw at rpi.edu> writes:
>
>> "every corner of the language"? Please. Why are you so post-happy
>> when it's quite obvious that you don't know enough about Lisp to
>> attack it?
>
> In addition to macros that define classes or methods, a common macro
> is the WITH-* macro, which sets up some kind of context, runs the body
> inside that context, and then does some cleanup.
>
> For example, my Lisp vendor, LispWorks, provides the macro MP:WITH-LOCK :
>
> (mp:with-lock (*the-big-lock*)
> (do-something-atomic)
> (something-else)
> (almost-odone))
>
> The generated first seizes a process lock called *THE-BIG-LOCK*, runs
> the code in the body and then releases the lock. I never have to
> worry that I've taken a lock without releasing it, because LispWorks
> has encoded that behaviour into MP:WITH-LOCK (and if they handn't,
> this macro is trivial to write).
>
> Now I can tell if I'm keeping a lock around too long because all the
> code that appears inside this WITH-LOCK is all the code that I'm
> locking. Further, the generated code is surrounded by an
> UNWIND-PROTECT, which means that if an error is raised or anything
> abnormal happens in the dynamic scope of the UNWIND-PROTECT, Lisp will
> run cleanup code as it flees back up the stack. So if there is an
> error in my code, it does not prevent other processes from seizing
> that lock, because it will be released as the error is signaled.
>
> Here is the expansion:
>
> CL-USER 7 > (write (macroexpand '(mp:with-lock (*the-big-lock*)
> (do-something-atomic)
> (something-else)
> (almost-odone)))
> :pretty t :case :downcase)
> (let ((#:g17553 *the-big-lock*))
> (when (mp:process-lock #:g17553)
> (unwind-protect
> (progn (do-something-atomic) (something-else) (almost-odone))
> (mp::in-process-unlock #:g17553))))
Now it happens what Graham described. The blub programmer will explain
that this is not needed, because he can do the same thing in blub.
In Python you could write a function withLock that takes a lock and a
function.
Then you can say
def throwAway001():
doSomethingAtomic()
somethingElse()
almostOdone()
withLock(theBigLock, throwAway001)
or maybe
withLock(theBigLock,
lambda ():
doSomethingAtomic()
somethingElse()
almostOdone())
So Lisp saves you the "lambda ():" or "def throwAway001():".
That is nice I would say. Why write it if the computer could do that for
you?
André
--
More information about the Python-list
mailing list