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 14:21:42 +0200 Message-ID: <00426c84-76ad-43b3-99ca-4b8e345d6725@www.fastmail.com> References: <87im5155pu.fsf@web.de> 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="35323"; 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 14:22:52 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 1lTOFo-00094S-Hf for guile-user@m.gmane-mx.org; Mon, 05 Apr 2021 14:22:52 +0200 Original-Received: from localhost ([::1]:38430 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lTOFn-0004bZ-Ir for guile-user@m.gmane-mx.org; Mon, 05 Apr 2021 08:22:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34224) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTOF7-0004ZG-JM for guile-user@gnu.org; Mon, 05 Apr 2021 08:22:09 -0400 Original-Received: from wout5-smtp.messagingengine.com ([64.147.123.21]:53187) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lTOF5-0000Iw-KP for guile-user@gnu.org; Mon, 05 Apr 2021 08:22:09 -0400 Original-Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 4331116E0; Mon, 5 Apr 2021 08:22:04 -0400 (EDT) Original-Received: from imap42 ([10.202.2.92]) by compute6.internal (MEProxy); Mon, 05 Apr 2021 08:22:04 -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=GSRWt G3MLAtZneKRBXsW3uAEOC/+T6S+97C+ycnwRaE=; b=HmbQiUM2RUXJphaX0PyEd dN2ptsh6gRjnNbbBVHzKnHHMPLBWrATfLtbx+iVj939+ktTEhnpqr0azH0O/KNa5 1BRldWrThrE0jIODMpMqG4RWWxrryKQPrPynE4PjKp2JWFxtuOBRaH140QWsKtU4 5nPpvdKSswz1KUOkWfWT4upnxkqaPuYSjBU7bbG7TkTLYzKPKXASop4/7L9LIqL/ OMnHSys5w72OBJRyxmf31rwRvNCIXGepQX2AXRviDeb538NMopAus2Qt4Dukkbt2 x9UG5RmG64LbchOuXEONEmL//iNM2MS2KhUV2ZKmtEIBemiV7/ntaFyJquiD4sE9 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=GSRWtG3MLAtZneKRBXsW3uAEOC/+T6S+97C+ycnwR aE=; b=LGjtGJnLhe8zCpk6+vuwgRYfYKEJ4/GQYfy0eeCir7LVHLNFxHNO84Lkf qLDGpp0MFjbBSCB3/2GZdQlWqCVnrlaeq7+PsIFMWB19vp4UKEooL+iuC0wdYxJk orqVpguEbVMrCPfDtf0EYzMa1LpnrxcvM8KpUZHAEH/jGRH7nQKMGl4exPXOkpX5 GjYQ/Vmjtx1HdLKohiJcbjBIH6kTv/iLEp0O1HlyxswFSVUAEFv0m4kjrIYWWpr1 YzWAINdt11b3zvTiFgyDaMziLreBLhXNmcb78UUBrgXx6ssTLZg+YqJu1ZbzKI6V G/ymPtU3Ax5ZboOnco/MtNwGqLUeA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrudejvddghedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpefnihhn uhhspgeujhpnrhhnshhtrghmuceolhhinhhushdrihhnthgvrhhnvghtsehfrghsthhmrg hilhdrshgvqeenucggtffrrghtthgvrhhnpeekleevieevleejffetieeltdelkeevtedu heefheduieffffdttdegjeeihedtjeenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehlihhnuhhsrdhinhhtvghrnhgvthesfhgrshhtmhgrihhl rdhsvg X-ME-Proxy: Original-Received: by mailuser.nyi.internal (Postfix, from userid 501) id 7B039310005D; Mon, 5 Apr 2021 08:22:03 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface In-Reply-To: <87im5155pu.fsf@web.de> 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:17392 Archived-At: Can you use the procedural part of syntax-rules? You have the power of u= sing scheme at expansion time, which means you could do list-ref all you= want. The only thing is that guile lacks syntax->list, so sometimes you have t= o manually turn it into a list. Say you are matching ((_ stuff ...) Body= ) stuff is a syntax object. You could turn it into a list of syntax obje= cts by doing #'(stuff ...). Then you can treat it as a regular list, an= d use quasisyntax to put it back into your output syntax.=20 Writing this on my phone. Sorry for the brevity (and lack of code). --=20 Linus Bj=C3=B6rnstam 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 fr= om > user-input. Currently I have multiple macros, and the correct one (whi= ch > 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 executed= > only during final processing, and when I evaluate any of the > consequences (as it happens with procedure-arguments), then the timing= > of the code execution does not match anymore. So I must absolutely do > this in macros. >=20 >=20 > I=E2=80=99ve tried to get that working, but all my tries failed. Is th= ere 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 bottom)= , > but I would like to: >=20 > - replace Respond2 and Respond3 by something recursive, so resp can ha= ve > 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