Re: What you can do with macros in Lisp
From: Pascal Bourguignon (spam_at_thalassa.informatimago.com)
Date: 10/12/03
- Next message: Alan Crowe: "Re: Newbie - request for code comments"
- Previous message: Howard Ding: "Re: Newbie - request for code comments (thanks)"
- In reply to: Tim Bradshaw: "What you can do with macros in Lisp"
- Next in thread: Kenny Tilton: "Re: What you can do with macros in Lisp"
- Reply: Kenny Tilton: "Re: What you can do with macros in Lisp"
- Reply: Tim Bradshaw: "Re: What you can do with macros in Lisp"
- Reply: Kaz Kylheku: "Re: What you can do with macros in Lisp"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 12 Oct 2003 16:09:44 +0200
Tim Bradshaw <tfb@cley.com> writes:
> Someone mentioned that there's been the usual futile argumentation
> between Lisp people and you-don't-need-macros-at-all and/or
> you-can-do-it-all-with-HOFs people.
>
> Here's something I'd like to see non-macro people do.
>
> Imagine you have a language with a CASE expression. You're writing
> (or generating: for instance in the output of some kind of FSM system)
> code that has a huge number of large, dense, integer CASE expressions,
> which can obviously be optimised into jump-tables. But your compiler
> won't do this, so your code is spending lots of time testing integers
> repeatedly.
>
> So: implement NCASE, which looks exactly like your language's CASE
> expression, but, if all the keys are numeric and if they pass some
> user-definable test - for instance that there are enough keys and they
> are dense enough in their range, will expand to a jump-table.
> Otherwise it should just expand to the ordinary CASE expression.
>
> Can you do this with macros in Lisp? Yes: in fact I did this ages
> ago, with results I've now put up at
> http://www.tfeb.org/lisp/toys.html#NCASE.
>
> Can you do it any other way (in partiucular: in any way which isn't
> equivalent to writing a macro system...)
>
> --tim
Well, as explained in Paul Graham's "On Lisp" in his extended coverage
of LISP macros, if you don't mind the typing (and most importantly the
associated obfuscation by redundant details), you can always replace
macro calls by function calls encapsulating the bodies in closure
lambdas (or even, merely quoting them and let the called function make
a function of these lists).
The most difficult stuff is the manipulation of variables (lexical
scope) and declarations in general. But since LISP is highly
(totally?) dynamic, you can always generate a declaration at run-time
rather than compilation time. Well, perhaps doing compilation-time
stuff at run-time is the equivalent of writing a macro system...
http://lib1.store.vip.sc5.yahoo.com/lib/paulgraham/onlisp.pdf
(defmacro with-var (varname value &body body)
`(let ((,varname ,value)) ,@body))
(with-var x (* 6 7)
(format t "x =~D~%" x)
(format t "2x =~D~%" (* 2 x)))
x =42
2x =84
NIL
(defun f-with-var-1 (varname value body)
(set varname value) ;; wrong, we're setting a global variable
(funcall body))
(f-with-var-1 'x (* 6 7)
(lambda () (format t "x =~D~%" x)
(format t "2x =~D~%" (* 2 x))))
x =42
2x =84
NIL
(format t "x= ~D~%" x)
x= 42
NIL
(makunbound 'x)
;;X
(defun f-with-var-2 (varname value body)
;; cheating or not?
(eval (list `(lambda (,varname) (progn ,@body)) value)))
(f-with-var-2 'x '(* 6 7)
'((format t "x =~D~%" x)
(format t "2x =~D~%" (* 2 x))))
x =42
2x =84
NIL
Of course, if you add the constraint of not having to change the
source and quote everything, it's trivially true that you cannot do
what you're asking without a macro system.
I note that when I program in languages other than LISP, my sources
are covered by comments containing emacs lisp code, followed by some
automatically generated code...
-- __Pascal_Bourguignon__ http://www.informatimago.com/ Do not adjust your mind, there is a fault in reality.
- Next message: Alan Crowe: "Re: Newbie - request for code comments"
- Previous message: Howard Ding: "Re: Newbie - request for code comments (thanks)"
- In reply to: Tim Bradshaw: "What you can do with macros in Lisp"
- Next in thread: Kenny Tilton: "Re: What you can do with macros in Lisp"
- Reply: Kenny Tilton: "Re: What you can do with macros in Lisp"
- Reply: Tim Bradshaw: "Re: What you can do with macros in Lisp"
- Reply: Kaz Kylheku: "Re: What you can do with macros in Lisp"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|