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

Re: A (possibly interesting) macro



Occasionally I find myself trying to read nested functions in the
"wrong direction."  For example, a form such as:

         (BAR (PLUS (FOO (REVERSE X)) 1))

I might understand as "first reverse x, apply FOO to that list,
add 1 to the result and call BAR on that."  This is precisely
backwards.  For cases where this kind of thinking seems more
natural, the SEQ macro allows you to write:

(SEQ (REVERSE X)
     (FOO *)
     (PLUS * 1)
     (BAR *))

which expands into the above code.  Its hard to characterize
the places where this kind of SEQuential, non-functional thinking
is appropriate, but there definitely seem to be some.

Note that any middle ground between totally SEQuential and totally
functional is possible.  We could have described the above as:

(SEQ (FOO (REVERSE X))
     (BAR (PLUS * 1)))

or even as:

(SEQ (BAR (PLUS (FOO (REVERSE X)) 1)))        [!]


A definition of SEQ is:

(defmacro seq (&rest forms &aux replacement)
  (mapc
    #'(lambda (form)
	(setq replacement
	  (cons (car form) (subst replacement '* (cdr form)))))
    forms)
  replacement)