home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / lisp / 2298 < prev    next >
Encoding:
Text File  |  1992-08-25  |  1.9 KB  |  58 lines

  1. Newsgroups: comp.lang.lisp
  2. Path: sparky!uunet!caen!hellgate.utah.edu!hellgate!moore
  3. From: moore@cs.utah.edu (Tim Moore)
  4. Subject: Re: Can I use EVAL?
  5. Message-ID: <MOORE.92Aug25153642@defmacro.cs.utah.edu>
  6. Followup-To: comp.lang.lisp
  7. In-reply-to: danny@farallon.com's message of 25 Aug 92 19:52:35 GMT
  8. Organization: University of Utah CS Dept
  9. References: <danny-250892143222@danny.farallon.com>
  10. Date: 25 Aug 92 15:36:42
  11. Lines: 45
  12.  
  13. In article <danny-250892143222@danny.farallon.com> danny@farallon.com (Danny Brewer) writes:
  14.  
  15.    ;;; Now compile the pattern into a LAMBDA using compiling matcher
  16.  
  17.    ? (Compile-Match '(a ?x c))
  18.    (LAMBDA (DATUM ...)  [...a bunch of code deleted...] )
  19.  
  20.    ? (SETF *p* *)           ; save pattern code in a variable
  21.    ? (SETF *d* '(a b c))    ; set datum variable
  22.  
  23.    Now that I've got the LAMBDA in the variable *p*, how can I do
  24.    anything with it?  I can't FUNCALL or APPLY it.  But I can
  25.    use a really ugly EVAL as follows:
  26.  
  27.    ? (EVAL `(,*p* ',*d*))
  28.    ((?X . B))
  29.  
  30.    This works okay, but yucko!
  31.  
  32. (eval `#',*p*) will return a function. In cltl2 lisps, you can coerce
  33. the lambda expression returned by compile-match to a function:  
  34.  
  35. (coerce *p* 'function)
  36.  
  37. These solutions use eval explicitly or implicitly, but that's OK. eval
  38. was made for this sort of thing. If you are translating little
  39. languages to lisp code, the prohibition against using eval breaks
  40. down.
  41.  
  42. You could compile the lambda expression:
  43. (compile nil *p*)
  44.  
  45.  
  46. Alternatively, you could think about defining your patterns in a
  47. declarative manner so that the lambda expressions will be dealt with
  48. at compile or load time:
  49.  
  50. (defmacro defpattern (pattern)
  51.   `(eval-when (:compile-toplevel :execute)
  52.      (add-pattern-to-db ,pattern #',(compile-match pattern))))
  53.  
  54. --
  55. Tim Moore                    moore@cs.utah.edu {bellcore,hplabs}!utah-cs!moore
  56. "Wind in my hair - Shifting and drifting - Mechanical music - Adrenaline surge"
  57.     - Rush
  58.