Re: What am I doing wrong?


On Sep 24, 2004, at 1:19 AM, Rohan NIcholls wrote:
I have a problem that probably has to do with my not understanding the
semantics of first-class objects, and calling them.  Or something as
stupid as a syntax error.

Here is macro to map a workspace to a key

(defmacro map-workspace ( num )
  (let ((g-num (gensym)))
    (let ((g-num num))
`(bind-keys global-keymap ,(concat "M-" (number->string g-num)) '(activate-workspace ,g-num)))))

there's no real reason to write this as a macro, e.g. since it doesn't rely on `num' not being evaluated in some cases, so you could easily turn it into a function:

(defun map-workspace (num)
(bind-keys global-keymap (concat "M-" (number->string num)) `(activate-workspace ,num)))

then because I am really lazy I thought I would do this:
(defun map-workspaces ()
  (do ((x 1 (1+ x)))
      ((> x 7))
;;    (princ x)))
     (map-workspace x)))

This does not work, however if I switch it with the commented out
princ statement it works.

the problem is that (map-workspace x) fails to macro-expand, since (number->string 'x) isn't valid, using the above function definition should work. (The reason it's 'x there is because macros see their arguments _before_ they are evaluated)

btw, to experiment with macros, it's useful to run rep and use the ,expand command to see what the code they actually generate (that's how I worked out what's happening here)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]