From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Linus_Bj=C3=B6rnstam?= Newsgroups: gmane.lisp.guile.user Subject: Re: Syntax-Case macro that selects the N-th element from a list Date: Mon, 05 Apr 2021 15:40:29 +0200 Message-ID: <8e6a1a19-c857-44c2-b5ea-3801f9366677@www.fastmail.com> References: <87im5155pu.fsf@web.de> <00426c84-76ad-43b3-99ca-4b8e345d6725@www.fastmail.com> Mime-Version: 1.0 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="18824"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Cyrus-JMAP/3.5.0-alpha0-273-g8500d2492d-fm-20210323.002-g8500d249 To: "Dr. Arne Babenhauserheide" , "Guile User Mailing List" Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Mon Apr 05 15:41:11 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lTPTa-0004mN-Ol for guile-user@m.gmane-mx.org; Mon, 05 Apr 2021 15:41:10 +0200 Original-Received: from localhost ([::1]:41250 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lTPTZ-0005Nl-Mn for guile-user@m.gmane-mx.org; Mon, 05 Apr 2021 09:41:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51582) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTPTO-0005Na-2e for guile-user@gnu.org; Mon, 05 Apr 2021 09:40:58 -0400 Original-Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:37113) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTPTL-0004y8-21 for guile-user@gnu.org; Mon, 05 Apr 2021 09:40:57 -0400 Original-Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 35A04189E; Mon, 5 Apr 2021 09:40:53 -0400 (EDT) Original-Received: from imap42 ([10.202.2.92]) by compute6.internal (MEProxy); Mon, 05 Apr 2021 09:40:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.se; h= mime-version:message-id:in-reply-to:references:date:from:to :subject:content-type:content-transfer-encoding; s=fm2; bh=3fQOj HeREO+mUo3ozue3IWAtqPuQHnUrL9/gujd6Ifs=; b=Tk764V6ArzqKVDueji6xK NHTL2WwhPFQY+NM23g/vtBvWImiN2oHpHBJwqrvBCNdFtW1CnFDSlKdlBurR893V e6zpx22e8L501iu8nC0yf61UL8aD/cXqIr++82uoIK+40fswaOHeGPazYiovoB6+ p4xAQHuYwrGMObZqqFf4VNz3Nk5cQAQpP+Feo9+8gaor+2ebF0b2poovo+tAC+eH dt5WVN8EBmGaUMeiweAHtQKVBrljX5wGBjGo7Sb3cBO7NXGcpfuLKKsEFXlZJpei LHErIgs9x4Zu4UZ/tVIgsz4/HxD5xpf+jjD2jS/9u7wuPfK9knWiXn+ppi1iNRA1 w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm2; bh=3fQOjHeREO+mUo3ozue3IWAtqPuQHnUrL9/gujd6I fs=; b=ecph2Q3DhfCjnM3dQ1AuI5Fm3+VB+MQ2Zyc9oMNp36DfTNvwtqJ1Y1QqA GrVIuzhY9ey1P/GPiTKJZGexmXFYtJQaOdgzKUQrdoXONJgRMLoseqBMPGyXQnMt ItEjC7G4uv+JjN6IEGl8J52o4gSV1PbsSLgAyTU+xulO2yF7kYOo24bALT/XFUJS gYdCZe3dBwQwAd/p7baPzqreFfIsa/wpqUxWiNSxabCYHQUf7qHZrH04rDZ/dhcl m8Q1RJceNIG8FX7qS/TuGpnY9WvmQe4CmccXx/AfBX6knEv6GqdiE4i5uq/9d/0V YWMNI9raNykIWmYA4VPXCIoRer53w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudejvddgieekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpefnihhn uhhspgeujhpnrhhnshhtrghmuceolhhinhhushdrihhnthgvrhhnvghtsehfrghsthhmrg hilhdrshgvqeenucggtffrrghtthgvrhhnpeekleevieevleejffetieeltdelkeevtedu heefheduieffffdttdegjeeihedtjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehlihhnuhhsrdhinhhtvghrnhgvthesfhgrshhtmhgrihhl rdhsvg X-ME-Proxy: Original-Received: by mailuser.nyi.internal (Postfix, from userid 501) id CB275310005D; Mon, 5 Apr 2021 09:40:51 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface In-Reply-To: <00426c84-76ad-43b3-99ca-4b8e345d6725@www.fastmail.com> Received-SPF: pass client-ip=64.147.123.21; envelope-from=linus.internet@fastmail.se; helo=wout5-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17393 Archived-At: That "syntax-rules" is of course syntax-case. Try writing it first with unhygienic macros and get that working before = porting to syntax-case if you don't know the ins-and-outs of syntax-case= . --=20 Linus Bj=C3=B6rnstam On Mon, 5 Apr 2021, at 14:21, Linus Bj=C3=B6rnstam wrote: > Can you use the procedural part of syntax-rules? You have the power of= =20 > using scheme at expansion time, which means you could do list-ref all=20= > you want. >=20 > The only thing is that guile lacks syntax->list, so sometimes you have= =20 > to manually turn it into a list. Say you are matching ((_ stuff ...)=20= > Body) stuff is a syntax object. You could turn it into a list of synta= x=20 > objects by doing #'(stuff ...). Then you can treat it as a regular=20= > list, and use quasisyntax to put it back into your output syntax.=20 >=20 > Writing this on my phone. Sorry for the brevity (and lack of code). >=20 > --=20 > Linus Bj=C3=B6rnstam >=20 > On Mon, 5 Apr 2021, at 13:30, Dr. Arne Babenhauserheide wrote: > > Hi, > >=20 > > In dryads-wake I need selection of the element in a list in a macro = from > > user-input. Currently I have multiple macros, and the correct one (w= hich > > strips the non-selected choices) is selected in a simple cond: > >=20 > > (define-syntax-rule (Choose resp . choices) > > "Ask questions, apply consequences" > > (cond > > ((equal? resp 1) ;; resp is user-input. It is a natural number. > > (Respond1 choices)) > > ((equal? resp 2) > > (Respond2 choices)) > > ((equal? resp 3) > > (Respond3 choices)) > > (else > > #f))) > >=20 > > For this however I have three syntax-case macros: > >=20 > > (define-syntax Respond1 > > (lambda (x) > > (syntax-case x () > > ((_ ((question consequences ...) choices ...)) > > #`(begin > > (respond consequences ...))) > > ((_ (choices ...)) > > #`(begin #f))))) > >=20 > > (define-syntax Respond2 > > (lambda (x) > > (syntax-case x () > > ((_ (choice choices ...)) > > #`(begin > > (Respond1 (choices ...)))) > > ((_ (choices ...)) > > #`(begin #f))))) > >=20 > > (define-syntax Respond3 > > (lambda (x) > > (syntax-case x () > > ((_ (a b choices ...)) > > #`(Respond1 (choices ...))) > > ((_ (choices ...)) > > #`(begin #f))))) > >=20 > >=20 > > I would like to get rid of those three definitions and replace them = by > > at most two (one that strips N initial list entries, and Respond1). > >=20 > > I cannot move to procedures, because I have code that must be execut= ed > > only during final processing, and when I evaluate any of the > > consequences (as it happens with procedure-arguments), then the timi= ng > > of the code execution does not match anymore. So I must absolutely d= o > > this in macros. > >=20 > >=20 > > I=E2=80=99ve tried to get that working, but all my tries failed. Is = there a way > > and can you show it to me? > >=20 > > This is a minimal working example. The output should stay the same, > > except for part 4, which needs this change to work (see at the botto= m), > > but I would like to: > >=20 > > - replace Respond2 and Respond3 by something recursive, so resp can = have > > arbitrary high values (not infinite: max the length of the options= ) and > > - replace the cond-clause by a call to the recursive macro. > >=20 > > (define-syntax-rule (respond consequence consequence2 ...) > > (begin > > (write consequence) > > (when (not (null? '(consequence2 ...))) > > (write (car (cdr (car `(consequence2 ...)))))))) > >=20 > > (define-syntax Respond1 > > (lambda (x) > > (syntax-case x () > > ((_ ((question consequences ...) choices ...)) > > #`(begin > > (respond consequences ...))) > > ((_ (choices ...)) > > #`(begin #f))))) > >=20 > > (define-syntax Respond2 > > (lambda (x) > > (syntax-case x () > > ((_ (choice choices ...)) > > #`(begin > > (Respond1 (choices ...)))) > > ((_ (choices ...)) > > #`(begin #f))))) > >=20 > > (define-syntax Respond3 > > (lambda (x) > > (syntax-case x () > > ((_ (a b choices ...)) > > #`(Respond1 (choices ...))) > > ((_ (choices ...)) > > #`(begin #f))))) > >=20 > >=20 > > (define-syntax-rule (Choose resp . choices) > > "Ask questions, apply consequences" > > (cond > > ((equal? resp 1) > > (Respond1 choices)) > > ((equal? resp 2) > > (Respond2 choices)) > > ((equal? resp 3) > > (Respond3 choices)) > > (else > > #f))) > >=20 > >=20 > > (display "Choose 1: should be bar:") > > (Choose 1 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 2: should be warhar:") > > (Choose 2 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 3: should be mar:") > > (Choose 3 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 4: should be tar:") > > (Choose 4 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > > (display "Choose 5: should be #f:") > > (Choose 5 (foo 'bar) (foo 'war 'har) (foo 'mar) (foo 'tar)) > > (newline) > >=20 > >=20 > > Best wishes, > > Arne > > --=20 > > Unpolitisch sein > > hei=C3=9Ft politisch sein > > ohne es zu merken > >=20 > > Attachments: > > * signature.asc >=20 >