From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: Repeat syntax Date: Sat, 25 Nov 2017 15:34:24 +0000 Message-ID: <20171125153424.6b2e948d@bother.homenet> References: <1511586326.26826.3.camel@qlfiles.net> <20171125152137.4ee1d267@bother.homenet> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1511624102 31332 195.159.176.226 (25 Nov 2017 15:35:02 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 25 Nov 2017 15:35:02 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Sat Nov 25 16:34:56 2017 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eIcTl-0007VF-JG for guile-user@m.gmane.org; Sat, 25 Nov 2017 16:34:53 +0100 Original-Received: from localhost ([::1]:53499 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eIcTr-0004dF-8I for guile-user@m.gmane.org; Sat, 25 Nov 2017 10:34:59 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53451) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eIcTQ-0004cm-EE for guile-user@gnu.org; Sat, 25 Nov 2017 10:34:33 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eIcTL-0005DH-IP for guile-user@gnu.org; Sat, 25 Nov 2017 10:34:32 -0500 Original-Received: from mail-wr0-x22c.google.com ([2a00:1450:400c:c0c::22c]:39530) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eIcTL-0005Cd-BB for guile-user@gnu.org; Sat, 25 Nov 2017 10:34:27 -0500 Original-Received: by mail-wr0-x22c.google.com with SMTP id 11so19277783wrb.6 for ; Sat, 25 Nov 2017 07:34:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=xCnI3zqV/8F9wYCHKI+jxIFFH3KuMhfyb9CPti5jhCw=; b=htG6xqSaVOAF/GJy4Gt4MxSEXDPqioTeON7Sn3nTDH56glOnczmA2A+hcrFCsO0yUh 0W09p+lH3+A35ZmhPJxf+Gwzej2xERUHCSmQtugCUYGmk+89OCbEmv2c+Ief6fM98RZK vYvL2dw2tewXSzDAzGJdLshWuun0wJM1YdRmxpOF+5XbVsZ/mQKRWQGuTQ53ENiM05K9 2X+m9yZdDjre4o9dTeUf9IAZ6GWJUxCl7eWuuaX9GbjRgbLRXAkoxgD39PSh8CMy5RaD QFiyKrwqu3I09hiikOyroyqqo7UaX0IQo1+AF/9nhHLY/jUOP61WnjtingTUvyI6pMmP PI5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xCnI3zqV/8F9wYCHKI+jxIFFH3KuMhfyb9CPti5jhCw=; b=AAKFGIoMX6YgPdbfnyzl5j+/gdbpGm65bKx8IVkeN4jzsfiHsqjCXIkwuKRK+eUGFT 0RmfcNVfpU22Mwu21pCfomMQhsjwpzLrHgDo2maJkH96slqTqADbktjGAYxTxL3F0/lw 09oupjN7eC6c0IhOAoysM/w5G/sacmxKqit51KlpPS6S80kh3IGfiUYB3HEcOm+BX5Yl EkvPH8siypJgN6u9ZIXYbS34yX1Ej2cpktkFzp5RwBU8dkn2m2eNOkjA83zAR+VtbXhU 7hunjctSgJAC0jb5IO9LeFuQATh+eCqipjM92mdlcseQIaVxCQIMMzBvHnwB8KGC8Inc 5pIQ== X-Gm-Message-State: AJaThX7Prf8jnOpTYgjcGQ7ZGsvotVqM7U167G280IZ9EECueohmRSKD imAXVOmSn13jlBYdS5lyyAT5Ww== X-Google-Smtp-Source: AGs4zMaeSXz+5cqKaoBVFSwxnH96yXJPHy4aDwjGPgSBecIwrRJCgnRRBVN3sMhAPJdztRGqk+BPBA== X-Received: by 10.223.199.70 with SMTP id b6mr17488036wrh.25.1511624065952; Sat, 25 Nov 2017 07:34:25 -0800 (PST) Original-Received: from bother.homenet ([95.146.110.48]) by smtp.gmail.com with ESMTPSA id h7sm10012240wrb.35.2017.11.25.07.34.24 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 25 Nov 2017 07:34:25 -0800 (PST) Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with ESMTP id 5BC9B260232 for ; Sat, 25 Nov 2017 15:34:24 +0000 (GMT) In-Reply-To: <20171125152137.4ee1d267@bother.homenet> X-Mailer: Claws Mail 3.14.1 (GTK+ 2.24.31; x86_64-unknown-linux-gnu) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::22c X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14291 Archived-At: On Sat, 25 Nov 2017 15:21:37 +0000 Chris Vine wrote: > On Fri, 24 Nov 2017 20:05:26 -0900 > Christopher Howard wrote: > > Hi list, I want to have a function > >=20 > > (repeat n exp exp* ...) > >=20 > > 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 > >=20 > > (define-syntax repeat > > =C2=A0 (syntax-rules (repeat) > > =C2=A0=C2=A0=C2=A0=C2=A0((_ n exp exp* ...) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0'(unless (<=3D n 0) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0exp > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0exp* > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0... > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0(repeat (- n 1) exp exp* ...)= )))) > >=20 > > 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=C2=A0either infinite expansion, or an important variable > > getting overshadowed. So, could somebody point me in the right > > direction? =20 >=20 > You need to separate the looping, which occurs at compile-time, from > evaluation of the expressions, which occurs at run-time. >=20 > Here is one way of doing it, insofar as I understand what you want: >=20 > (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))]))) >=20 > (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