home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / lisp / 2921 < prev    next >
Encoding:
Text File  |  1992-11-19  |  3.0 KB  |  65 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!europa.asd.contel.com!awds.imsd.contel.com!llyene!jato!ufo!Aig.Jpl.Nasa.Gov!charest
  3. From: charest@Aig.Jpl.Nasa.Gov (Len Charest)
  4. Subject: Re: Help: apply for macros ???
  5. Message-ID: <1992Nov19.222419.4597@jpl-devvax.jpl.nasa.gov>
  6. Sender: usenet@jpl-devvax.jpl.nasa.gov (For NNTP so rrn will be able to post)
  7. Nntp-Posting-Host: ai-cyclops
  8. Reply-To: charest@aig.jpl.nasa.gov
  9. Organization: NASA/Jet Propulsion Laboratory
  10. References:  <2610@bigfoot.first.gmd.de>
  11. Date: Thu, 19 Nov 1992 22:24:19 GMT
  12. Lines: 51
  13.  
  14. In article <2610@bigfoot.first.gmd.de>, wolf@prosun.first.gmd.de (Wolfgang Koehler) writes:
  15. [ asks if it is possible to APPLY #'F where F is the name of a macro. ]
  16.  
  17. [[ This question and an appropriate answer should appear in the FAQ list. ]]
  18.  
  19. The short answer is no. Recall that #'F is shorthand for (FUNCTION F). However, F
  20. names a macro--not a function or a LAMBDA-expression--so it is an error to
  21. evaluate (FUNCTION F) (see CLtL2, p 116).* Furthermore, it is not permitted to
  22. write (APPLY F ...) either since "the global function value of that symbol [F] is
  23. used (but it is illegal for the symbol to be the name of a macro or special form."
  24. (CLtL2, p 145)
  25.  
  26. It is quite possible to work around this problem:
  27.  
  28. Layered workaround: if you have the source code for the original macro then you
  29. might want to rewrite it such that the macroexpansion is simply a call to a
  30. function. The function may passed to APPLY. Here, the only job of the 
  31. macroexpansion is to interpret, quote, rewrite or otherwise massage the arguments
  32. of the macro. The philosophy behind this approach can be summed up as "If I can 
  33. do it with a macro then I should also be able to do it with an equivalent function
  34. call." Example:
  35.  
  36. (defun define-widget (name color viscosity)
  37.   ...code to make a widget...)
  38.  
  39. ;;; this macro simply quotes its args
  40. (defmacro defwidget (name &key color viscosity)
  41.   ;;there is an underlying function to do the work
  42.   `(define-widget ',name ',color ',viscosity))
  43.  
  44. ;;; macroexpansion reveals the equivalent function call:
  45. (macroexpand-1 '(defwidget jello :color green)
  46. => (define-widget 'jello 'green nil)
  47.  
  48. Encapsulation workaround: you could simply define a function that calls your
  49. macro. One pitfall here is that the macro call will not evaluate its arguments,
  50. so if the macroexpansion quotes any arguments, an explicit call to EVAL
  51. around those arguments will be necessary to get the encapsulation to perform 
  52. correctly.
  53.  
  54. DEFSUBST workaround: you could redefine your macro as a DEFSUBST--that is, a
  55. function that is proclaimed inline. An inline function *is* a function, so it may
  56. be used with APPLY, but lexical calls to it will also be expanded inline by the
  57. compiler.
  58.  
  59. #' workaround: you could redefine the #' reader to do whatever you want. I don't
  60. recommend this approach. ;-)
  61. ..................................................
  62.                                   Len Charest, Jr.
  63.                  JPL Artificial Intelligence Group
  64.                           charest@aig.jpl.nasa.gov
  65.