unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Repeat syntax
@ 2017-11-25  5:05 Christopher Howard
  2017-11-25  8:39 ` Alex Vong
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Christopher Howard @ 2017-11-25  5:05 UTC (permalink / raw
  To: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 873 bytes --]

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?

-- 
https://emailselfdefense.fsf.org/en/

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  2017-11-25  5:05 Repeat syntax Christopher Howard
@ 2017-11-25  8:39 ` Alex Vong
  2017-11-25 14:47 ` Matt Wette
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Alex Vong @ 2017-11-25  8:39 UTC (permalink / raw
  To: Christopher Howard; +Cc: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 1015 bytes --]

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?

Do you want to evaluate the expressions during macro-expand-time or
run-time?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  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 15:21 ` Chris Vine
  2017-12-30 13:18 ` Taylan Ulrich Bayırlı/Kammer
  3 siblings, 1 reply; 8+ messages in thread
From: Matt Wette @ 2017-11-25 14:47 UTC (permalink / raw
  To: Christopher Howard; +Cc: Guile User Mailing List


> On Nov 24, 2017, at 9:05 PM, Christopher Howard <christopher.howard@qlfiles.net> 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?
> 
> -- 
> https://emailselfdefense.fsf.org/en/

you probably want named let

((_ n exp exp* ...)
 (let loop ((n n))
     (unless (<= n 0)
        exp exp* ...
        (loop (1- n)))





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  2017-11-25 14:47 ` Matt Wette
@ 2017-11-25 15:06   ` Matt Wette
  2017-11-25 16:48     ` Christopher Howard
  0 siblings, 1 reply; 8+ messages in thread
From: Matt Wette @ 2017-11-25 15:06 UTC (permalink / raw
  To: Matt Wette; +Cc: Guile User Mailing List


> On Nov 25, 2017, at 6:47 AM, Matt Wette <matt.wette@gmail.com> wrote:
> 
> you probably want named let
> 
> ((_ n exp exp* ...)
> (let loop ((n n))
>     (unless (<= n 0)
>        exp exp* ...
>        (loop (1- n)))

but not a broken one

((_ n exp exp* ...)
 (let loop ((cnt n))
   (unless (<= cnt 0)
      exp exp* ...
      (loop (1- cnt)))))





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  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:21 ` Chris Vine
  2017-11-25 15:34   ` Chris Vine
  2017-12-30 13:18 ` Taylan Ulrich Bayırlı/Kammer
  3 siblings, 1 reply; 8+ messages in thread
From: Chris Vine @ 2017-11-25 15:21 UTC (permalink / raw
  To: guile-user

On Fri, 24 Nov 2017 20:05:26 -0900
Christopher Howard <christopher.howard@qlfiles.net> 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"))



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  2017-11-25 15:21 ` Chris Vine
@ 2017-11-25 15:34   ` Chris Vine
  0 siblings, 0 replies; 8+ messages in thread
From: Chris Vine @ 2017-11-25 15:34 UTC (permalink / raw
  To: guile-user

On Sat, 25 Nov 2017 15:21:37 +0000
Chris Vine <vine35792468@gmail.com> wrote:
> On Fri, 24 Nov 2017 20:05:26 -0900
> Christopher Howard <christopher.howard@qlfiles.net> 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"))

By the way, this assumes that you _do_ want a macro, as in your original
attempt.  The macro above will carry out loop unrolling at compile time,
so may well not be what you want for large values of 'n'.  The
alternative, as someone else has mentioned, is to use a normal function
with a named let loop in it.

Chris



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  2017-11-25 15:06   ` Matt Wette
@ 2017-11-25 16:48     ` Christopher Howard
  0 siblings, 0 replies; 8+ messages in thread
From: Christopher Howard @ 2017-11-25 16:48 UTC (permalink / raw
  To: Matt Wette; +Cc: Guile User Mailing List

[-- Attachment #1: Type: text/plain, Size: 1280 bytes --]

On Sat, 2017-11-25 at 07:06 -0800, Matt Wette wrote:
> > On Nov 25, 2017, at 6:47 AM, Matt Wette <matt.wette@gmail.com>
> > wrote:
> > 
> > you probably want named let
> > 
> > ((_ n exp exp* ...)
> > (let loop ((n n))
> >     (unless (<= n 0)
> >        exp exp* ...
> >        (loop (1- n)))
> 
> but not a broken one
> 
> ((_ n exp exp* ...)
>  (let loop ((cnt n))
>    (unless (<= cnt 0)
>       exp exp* ...
>       (loop (1- cnt)))))
> 

I want both the looping and the expression evaluation to be happening
at run time. The target is to repeat the side effects of the
expressions n times, and the return values are irrelevant.

I think this last submission from Matt does exactly what I want, the
full version being

(define-syntax repeat
  (syntax-rules ()
    ((_ n exp exp* ...)
     (let loop ((cnt n))
       (unless (<= cnt 0)
         exp exp* ...
         (loop (1- cnt)))))))

scheme@(guile-user)>  (repeat 10 (display "la\n"))
la
la
la
la
la
la
la
la
la
la

The part that confuses me a little is why

(repeat 10 (display cnt))

doesn't break it. Must be that magical hygiene mentioned in the info page.

-- 
https://worldenglishbible.org/

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Repeat syntax
  2017-11-25  5:05 Repeat syntax Christopher Howard
                   ` (2 preceding siblings ...)
  2017-11-25 15:21 ` Chris Vine
@ 2017-12-30 13:18 ` Taylan Ulrich Bayırlı/Kammer
  3 siblings, 0 replies; 8+ messages in thread
From: Taylan Ulrich Bayırlı/Kammer @ 2017-12-30 13:18 UTC (permalink / raw
  To: Christopher Howard; +Cc: Guile User Mailing List

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



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-12-30 13:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 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).