home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / lisp / 2326 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  3.6 KB

  1. Path: sparky!uunet!gatech!asuvax!farallon!danny.farallon.com!user
  2. From: danny@farallon.com (Danny Brewer)
  3. Newsgroups: comp.lang.lisp
  4. Subject: Re: Can I use EVAL?
  5. Message-ID: <danny-280892103325@danny.farallon.com>
  6. Date: 28 Aug 92 16:05:17 GMT
  7. References: <danny-250892143222@danny.farallon.com> <MOORE.92Aug25153642@defmacro.cs.utah.edu>
  8. Sender: news@farallon.farallon.com
  9. Followup-To: comp.lang.lisp
  10. Organization: Farallon Computing, Inc.
  11. Lines: 90
  12. Nntp-Posting-Host: danny
  13.  
  14. Thanks to everyone who responded with postings or email.
  15.  
  16. The solution I liked best was (COMPILE NIL *p*) to produce a
  17. function that can be FUNCALLed.
  18.  
  19. I could see from several emails that I should have been more
  20. clear in my original posting about the fact that I cannot
  21. simply put #' in front of a LAMBDA in the pattern compiler
  22. and return a closure.  There is no LAMBDA embedded within
  23. the compiler that is returned.  The returned LAMBDA list
  24. is actually "computed" from the pattern and can vary
  25. greatly due to the fact that the matchers support nested
  26. patterns, anonymous variables, segment matching and pattern
  27. directives.
  28.  
  29. Since I got such a satisfying solution to my first ugly EVAL,
  30. here's another place I've used EVAL and wonder if there is
  31. an alternative.  (Note all of the following discussion applies
  32. to the interpretive matcher, the compiling matcher simply compiles
  33. function names or LAMBDAs into the resulting LAMBDA expression.
  34. The interpretive matcher EVAL's them.)
  35.  
  36. The pattern directives can contain predicates.  For example:
  37.  
  38.    (Match '(a (?:* ?x) (?:? ?n NUMBERP) (?:* ?y) (?:+ ?z SUBSETP '(a b c)))
  39.           '(a  b c d        5             e f g       a b a c b))
  40.  
  41. which results in the bindings:
  42.  
  43.    ((?Z A B A C B) (?Y E F G) (?N . 5) (?X B C D))
  44.  
  45. The pattern means:
  46.    Match an 'a
  47.    followed by zero or more items,
  48.    followed by a number,
  49.    followed by zero or more items,
  50.    followed by one or more items, all of which must be SUBSETP of '(a b c)
  51.  
  52. Other pattern directive examples would be:
  53.  
  54.    (?:? ?x > 5)
  55.    (?:@ ?_ CONSP)
  56.    (?:? ?str STRINGP)
  57.    (?:? ?n = *PRINT-BASE*)
  58.    (?:*  ?items   (LAMBDA (items) (SUBSETP (INTERSECTION items *x*) *y*)))
  59.  
  60. The last example would match some items which when intersected with
  61. the special variable *x* are a subset of special variable *y*.
  62.  
  63. Basically, the third item can be the name of a function (or LAMBDA),
  64. and additional arguments can be given, such as the > function being
  65. given the argument 5 to test if ?x > 5 in the first example.
  66.  
  67. I use the following function to see if the pattern directive can
  68. match a potential value...
  69.  
  70. (DEFUN Call-Predicate (patternDirective potentialValue)
  71.   "Call the predicate of the pattern directive to see if the value is
  72. okay."
  73.   (COND
  74.    ;; Is there a predicate to test?
  75.    ((PatDir-Predicate? patternDirective)
  76.     (EVAL `(,(PatDir-Predicate patternDirective)
  77.             ',potentialValue
  78.             ,@(PatDir-Params patternDirective))))
  79.    
  80.    ;; If no predicate to call, then return T
  81.    (T)
  82.    ))
  83.  
  84. (DEFUN PatDir-Predicate (pattern-directive)
  85.   (THIRD pattern-directive))
  86.  
  87. (DEFUN PatDir-Params (pattern-directive)
  88.   (CDDDR pattern-directive))
  89.  
  90. In the interpreter it is probably not wise to spend the time compiling
  91. the predicate function (or LAMBDA) and then applying it to the
  92. potential value and other params.  Is there any better solution than
  93. using this ugly backquoted EVAL?
  94.  
  95. If I were going to match the pattern more than once I would use the
  96. pattern compiler.  When the pattern interpreter is being used, the
  97. pattern is probably being matched one time only.
  98.  
  99. BTW, I plan on posting these matchers to the cambridge.apple.com ftp
  100. site sometime in the future.
  101.  
  102. Danny Brewer
  103. danny@farallon.com
  104.