[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Macro for replacing a placeholder in an expression

From: Maxime Devos
Subject: Re: Macro for replacing a placeholder in an expression
Date: Thu, 28 Jul 2022 02:55:33 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0

These macros all sound more complicated than necessary -- on the first one, I've sent you a message with sneek:

;; By: Maxime Devos

;; This does not recurse into #(...).
;; Also, such a construct does not nest well, you can't put a 
replace-result-placeholder inside a replace-result-placeholder meaningfully,
;; so I'm wondering why you're doing this, maybe your goal can be accomplished 
more robustly with a different method.
(eval-when (expand load eval)
  (define (replace-placeholder new code) ; <--- recursively transforms code to 
replace '<?>' by new
    (syntax-case code (<?>)
      (<?> new)
      ((x . y)
       #`(#,(replace-placeholder new #'x) . #,(replace-placeholder new #'y)))
      (rest #'rest))))

(define-syntax replace-result-placeholder
  (lambda (s)
    (syntax-case s (<?>) ; <?>: placeholder
      ((_ new code) (replace-placeholder #'new #'code)))))

(display (replace-result-placeholder
           (<?> bar))) ; -> bar

(I think thinking in terms of 'operations' and special-casing lambda etc would make things harder here)

As a bonus, this supports things like `((x . <?>) (z . w)) which aren't supported by the original macro as that macro assumed lists.


Attachment: OpenPGP_0x49E3EE22191725EE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]