guile-user
[Top][All Lists]
Advanced

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

Re: Repeat syntax


From: Chris Vine
Subject: Re: Repeat syntax
Date: Sat, 25 Nov 2017 15:21:37 +0000

On Fri, 24 Nov 2017 20:05:26 -0900
Christopher Howard <address@hidden> wrote:
> Hi list, I want to have a function
> 
>   (repeat n exp exp* ...)
> 
> That calls the expressions n times for side effect, like for-each, but
> without the bother of dealing with a list. It seems like somebody else
> must have thought of this before, but I couldn't find the equivalent
> procedure. After reading 6.10.2 I came up with this
> 
> (define-syntax repeat
>   (syntax-rules (repeat)
>     ((_ n exp exp* ...)
>      '(unless (<= n 0)
>        exp
>        exp*
>        ...
>        (repeat (- n 1) exp exp* ...)))))
> 
> Which doesn't work I think because repeat gets expanded infinitely
> many times. I was pondering other ways to do this, but they all seem
> to end in either infinite expansion, or an important variable getting
> overshadowed. So, could somebody point me in the right direction?

You need to separate the looping, which occurs at compile-time, from
evaluation of the expressions, which occurs at run-time.

Here is one way of doing it, insofar as I understand what you want:

(define-syntax repeat
  (lambda (x)
    (syntax-case x ()
      [(_ n exp exp* ...)
       (let loop ([count (syntax->datum #'n)])
         (if (< 0 count)
             #`(begin
                 exp
                 exp*
                 ...
                 #,(loop (- count 1)))
             #'#f))])))

(repeat 5 (display "exp0 ") (display "exp1 ") (display "exp1\n"))



reply via email to

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