guile-user
[Top][All Lists]
Advanced

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

Re: Repeat syntax


From: Taylan Ulrich Bayırlı/Kammer
Subject: Re: Repeat syntax
Date: Sat, 30 Dec 2017 14:18:35 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux)

Christopher Howard <address@hidden> writes:

> 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?

The 'do' syntax, although difficult to get used to, can do this:

    (do ((times 10 (- times 1)))
        ((zero? times))
      <body>)

Here's the specification of 'do':

    (do ((<variable [id]> <init-val [expr]> <successor-val [expr]>)
         ...)
        (<termination-condition [expr]> <finisher [expr]> ...)
      <command [expr]>
      ...)

The metasyntax "x ..." means ZERO or more occurrences of x.

Evaluation rules:

1. The <variable> and <init-val> pairs are bound as if in a 'let'.
2. The <termination-condition> is evaluated:
   A. If true, the <finisher>s are evaluated in order, the last result
      being the result of the 'do' expression.
   B. If false:
      a. The <command>s are evaluated in order.
      b. The <variable> and <successor-val> pairs are bound as if in a
         'let' (the previous bindings of <variable>s are available).
      c. Go to step 2.

Taylan



reply via email to

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