Why don't people like lisp?
Jock Cooper
jockc at mail.com
Wed Oct 22 12:52:42 EDT 2003
Pascal Costanza <costanza at web.de> writes:
> Andrew Dalke wrote:
>
> (with-open-file (f "...")
> ...
> (read f)
> ...)
>
> ...is much clearer than...
>
> try:
> f=open('...')
> ...
> f.read()
> ...
> finally:
> f.close()
>
> Why do I need to say "finally" here? Why should I even care about
> calling close? What does this have to do with the problem I am trying
> to solve? Do you really think it does not distract from the problem
> when you first encounter that code and try to see the forest from the
> trees?
>snip
Can you implement with-open-file as a function? If you could how would
it compare to the macro version? It would look something like:
(defun with-open-file (the-func &rest open-args)
(let ((stream (apply #'open open-args)))
(unwind-protect
(funcall the-func stream)
(close stream))))
(defun my-func (stream)
... operate on stream...
)
(defun do-stuff ()
(with-open-file #'my-func "somefile" :direction :input))
One of the important differences is that MY-FUNC is lexically isolated
from the environment where WITH-OPEN-FILE appears. The macro version
does not suffer this; and it is often convenient for the code block
in the WITH-OPEN-FILE to access that environment.
More information about the Python-list
mailing list