unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: taylanbayirli@gmail.com (Taylan Ulrich Bayırlı/Kammer)
To: Christopher Howard <christopher.howard@qlfiles.net>
Cc: Guile User Mailing List <guile-user@gnu.org>
Subject: Re: Repeat syntax
Date: Sat, 30 Dec 2017 14:18:35 +0100	[thread overview]
Message-ID: <87fu7s5p78.fsf@gmail.com> (raw)
In-Reply-To: <1511586326.26826.3.camel@qlfiles.net> (Christopher Howard's message of "Fri, 24 Nov 2017 20:05:26 -0900")

Christopher Howard <christopher.howard@qlfiles.net> 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



      parent reply	other threads:[~2017-12-30 13:18 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-25  5:05 Repeat syntax Christopher Howard
2017-11-25  8:39 ` Alex Vong
2017-11-25 14:47 ` Matt Wette
2017-11-25 15:06   ` Matt Wette
2017-11-25 16:48     ` Christopher Howard
2017-11-25 15:21 ` Chris Vine
2017-11-25 15:34   ` Chris Vine
2017-12-30 13:18 ` Taylan Ulrich Bayırlı/Kammer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fu7s5p78.fsf@gmail.com \
    --to=taylanbayirli@gmail.com \
    --cc=christopher.howard@qlfiles.net \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).