Subject: Re: Macros vs Functions and a question about check-type
From: Erik Naggum <erik@naggum.no>
Date: 2000/06/28
Newsgroups: comp.lang.lisp
Message-ID: <3171212063253918@naggum.no>

* Friedrich Dominicus <frido@q-software-solutions.com>
| I'm trying getting a bit more familiar with Common Lisp and write some
| small piecs of code. I come along with this ones:
| 
| (defun with-cwd (dir thunk)
|   (let ((old-wd (pwd)))
|     (unwind-protect
|         (progn
|           (change-directory dir)
|           (funcall thunk))
|       (change-directory old-wd))))
| 
| (defmacro with-cwd-m (dir thunk)
|   (let ((old-wd (gensym)))
|     `(let ((,old-wd (pwd)))
|        (declare (dynamic-extent ,old-wd))
|        (unwind-protect
|            (progn
|              (change-directory ,dir)
|              (funcall ,thunk))
|          (change-directory ,old-wd)))))
| 
| comments are ommitted frankly here.

  I'll supply my own: This is Scheme in Common Lisp guise.  Bad karma.

  In Common Lisp, one generally uses bodies in macros, not thunks.
  Thunks are very useful when you don't have macros in the first place.

(defmacro with-cwd (dir &body body)
  (let ((old-wd (gensym)))
    `(let ((,old-wd (pwd)))
       (unwind-protect
           (progn
             (change-directory ,dir)
	     ,@body)
         (change-directory ,old-wd)))))

| - I found the line with (declare (dynamic-exte... in On Lisp page
| 149, it seems to me that using it might be ok here too. But I'm not
| sure, can someone explain why it might be good or if I better should
| omitt it.

  I see no use for it, but that doesn't mean it won't help a compiler
  somewhere decide to do something useful with it.

| Now a somewhat related question but a bit away.  I want the  first
| parameter of the funtion/macro be a String and the second to be a
| function with no parameter.

  I think you're getting a simpler, more readable, and better
  fulfillment of that desire by embedding a body in the macro
  expansion rather than calling a thunk.

  On a side-note: I dislike Scheme more every time somebody fails to
  understand that Scheme was designed to experiment with programming
  methodologies and even paradigms and copies some of its ways as if
  they were gospel.  That _really_ bugs me, mostly because it is _so_
  har do undo such concrete-bound learning -- people who apparently
  have no experience or training in getting the abstract picture get
  so _obsessive_ about particulars, and some of those particulars in
  Scheme are just not good ideas to take with you outside of Scheme.

#:Erik
-- 
  If this is not what you expected, please alter your expectations.