Why don't people like lisp?

Matthew Danish mdanish at andrew.cmu.edu
Wed Oct 22 19:45:46 EDT 2003


On Wed, Oct 22, 2003 at 12:30:01PM -0400, Brian Kelley wrote:
> Your two examples do completely different things and the second is 
> written rather poorly as f might might not exist in the finally block. 
> It certainly won't if the file doesn't exist.
> 
> A better comparison would (*might*, I haven't used lisp in a while) be:
> 
> (with-open-file (f filename :direction :output :if-exists :supersede)
>     (format f "Here are a couple~%of test data lines~%")) =>  NIL
> 
> if os.path.exists(filename):
>    f = open(filename, 'w')
>    print >> f, "Here are a couple of test data lines"

How are these in any way equivalent?  Pascal posted his example with
try...finally and f.close () for a specific reason.  In your Python
example, the file is not closed until presumably the GC collects the
descriptor and runs some finalizer (if that is even the case).  This is
much different from the Lisp example which guarantees the closing of the
file when the body of the WITH-OPEN-FILE is exited.  In addition:

``When control leaves the body, either normally or abnormally (such as
by use of throw), the file is automatically closed. If a new output file
is being written, and control leaves abnormally, the file is aborted and
the file system is left, so far as possible, as if the file had never
been opened.''

http://www.lispworks.com/reference/HyperSpec/Body/m_w_open.htm#with-open-file

So in fact, you pointed out a bug in Pascal's Python example, and one
that is easy to make.  All this error-prone code is abstracted away by
WITH-OPEN-FILE in Lisp.

-- 
; Matthew Danish <mdanish at andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."




More information about the Python-list mailing list