• Re: when are macros useful?

    From B. Pym@21:1/5 to All on Thu Jul 4 22:04:48 2024
    Here is a very simple macro example which shows some of the features: destructuring, substituting into a template to produce a form,
    using (gensym) to invent a needed symbol. The purpose of this macro
    is to introduce a very simple language feature; a loop construct
    which executes a sequence of forms N times, where N is a parameter.
    We implement our new kind of loop, which we will call ``ntimes''
    in terms of the Common LISP ``dotimes' loop:

    (defmacro ntimes (count . forms) (let ((counter (gensym)))
    `(dotimes
    (,counter ,count) ,@forms)))

    Scheme

    (define-syntax ntimes
    (syntax-rules ()
    [(_ count forms ...)
    (do ((n 0 (+ n 1)))
    ((= n count))
    forms ...)]))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to B. Pym on Sat Jul 6 07:42:53 2024
    On Thu, 4 Jul 2024 22:04:48 -0000 (UTC), B. Pym wrote:

    (define-syntax ntimes
    (syntax-rules ()
    [(_ count forms ...)
    (do ((n 0 (+ n 1)))
    ((= n count))
    forms ...)]))

    I think it’s a sign of desperation when you hope that using different
    kinds of bracketing symbols will somehow make your code clearer. The screen/page is two-dimensional; why not make use of both dimensions in
    laying out your code?

    (define-syntax ntimes
    (syntax-rules ()
    ((_ count forms ...)
    (do ((n 0 (+ n 1)))
    ((= n count))
    forms ...
    ) ; do
    )
    ) ; syntax-rules
    ) ; define

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)