[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: Zelphir Kaltstahl
Subject: Re: Macro for replacing a placeholder in an expression
Date: Thu, 28 Jul 2022 10:23:48 +0000

Hello Maxime!

On 7/28/22 02:55, Maxime Devos wrote:

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.


I'll need to look at this and learn about eval-when. I also did not think about vectors yet. Thank you!

Best regards,


reply via email to

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