(define-syntax symbol??
   (syntax-rules ()
     ;; The check is done by first pattern matching against some other
     ;; forms, that are not a symbol.
     ((symbol?? (x . y) kt kf) kf) ; It's a pair, not a symbol
     ((symbol?? #(x ...) kt kf) kf) ; It's a vector, not a symbol

The first two cases don’t need any explanation I think.

     ;; After those things are excluded, the thing might be a symbol.
     ((symbol?? maybe-symbol kt kf)
      (let-syntax ((test
                    (syntax-rules ()
                      ((test maybe-symbol t f) t)
                      ((test x t f) f))))
        (test abracadabra kt kf)))))

If maybe-symbol is a symbol, then (test abracadabra kt kf) matches (test 
maybe-symbol t f) (let t=kt, f=kf, maybe-symbol=abracadabra).

If maybe-symbol is not a symbol, for example it is a string “hello”  (please 
ignore wrong quoting), then
(test maybe-symbol t f) becomes (test “hello” t f).  The string “hello” cannot 
act as an identifier (because it is a string, not a symbol), so (test 
abracadabra kt kf) does not match the first case (test maybe-symbol t f). Only 
the second case (test x t f) remains, and (test abracadabra kt kf) matches this 
(set x to abracadabra, t to kt, f to kf).

I think the first two cases are superfluous, but perhaps there is a performance 
advantage.

(I’m wondering if this still works in the case (symbol? ...), because ... is 
special in syntax-rules)

Best regards,
Maxime Devos.


Reply via email to