From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zelphir Kaltstahl Newsgroups: gmane.lisp.guile.user Subject: Re: Macro for replacing a placeholder in an expression Date: Thu, 28 Jul 2022 08:39:45 +0000 Message-ID: <61f3c2d2-78ec-cec6-5933-f0f8a5bc3aba@posteo.de> References: <80083c86-19b0-7537-be70-f763bd5b390f@posteo.de> <0c9ca13b-5cda-b575-4645-2040bb30ea53@telenet.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31315"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Guile User To: Maxime Devos Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Jul 28 11:20:45 2022 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 1oGzhF-0007xB-0Q for guile-user@m.gmane-mx.org; Thu, 28 Jul 2022 11:20:45 +0200 Original-Received: from localhost ([::1]:49816 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oGzhD-0001W2-K2 for guile-user@m.gmane-mx.org; Thu, 28 Jul 2022 05:20:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53582) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oGz3e-0005ha-T8 for guile-user@gnu.org; Thu, 28 Jul 2022 04:39:52 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:44489) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oGz3c-0003Qs-HM for guile-user@gnu.org; Thu, 28 Jul 2022 04:39:50 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 9A5CE240107 for ; Thu, 28 Jul 2022 10:39:46 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1658997586; bh=brLBiGL1JEPMqnAGFTgdmJCOjLJE8a565aUvMhNt1tY=; h=Date:Subject:To:From:Cc:From; b=NURwQih8daWFgUxgIg/TO47xyfyHe4Yf/Gnx8Y4rWnQ3PKDYTbAA1vlMWstfjUrJb 2HU/KX6fs/2SedTNnJfcFXdxR/arS79TThc6VzFoCHNJ0xmXA35muxBOaNkhqO/0eJ tSrJpkTrxELmL5OOWmTbv+GtL1QD7p7GsAsaw48p0cVa78gzmqXEMdxiw9g3+bGYYn OJZ5u+5DnQAf3RS1GM8Sx/oALYbH33/eehYgMeF8cS5iI0mBINCv2J0zjHPDrISPSz VbKjHk9QeWb9Vdjis/8f7CGkaJYGwGU7wG8Wtgf/y8ohunVMXNBAXdho5nskepC9N3 /RDyEBeBlcSiw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Ltkbj63Rqz6tmL; Thu, 28 Jul 2022 10:39:45 +0200 (CEST) Content-Language: en-US In-Reply-To: <0c9ca13b-5cda-b575-4645-2040bb30ea53@telenet.be> Received-SPF: pass client-ip=185.67.36.66; envelope-from=zelphirkaltstahl@posteo.de; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 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:18457 Archived-At: Hello Maxime! Thank you for your quick response! (Mailing list saves me again! Yay!) On 7/28/22 03:04, Maxime Devos wrote: > > On 28-07-2022 01:57, Zelphir Kaltstahl wrote: >> scheme@(guile-user)> (define-syntax test >>   (syntax-rules (lambda) >>     [(_ (op args body* ...)) >>      ((test op) (test args) (test body* ...))] >> >>     [(_ thing1 thing2 things* ...) >>      ((test thing1) (test thing2 things* ...))] >> >>     [(_ (thing)) >>      (thing)] >> >>     [(_ thing) >>      thing])) >> scheme@(guile-user)> (test (lambda (a) (+ a 1))) >> While compiling expression: >> Syntax error: >> unknown file:798:0: lambda: invalid argument list in subform ((a)) of (test (a)) >> ~~~~ >> >> There seems to be something about a template like (one-thing) that I do not >> understand or something completely different is going on. > > Here's what happening: > > (test (lambda (a) (+ a 1)) > > --> because  the 'test' in the beginning is a macro > > ((test lambda) (test (a)) (test (+ a 1)) > > --> likewise > > (lambda (test (a)) (test (+ a 1)) > > Now we end up with the 'lambda' macro. The lambda macro sees as argument list > (test (a)) and interprets 'test' as the first argument, but the second part > '(a)' is not an identifier so the lambda macro cannot do anything with that > and tells you that by saying: lambda: invalid argument list in .... Ahhh now I get it! lambda is also a macro … I did not think of that. > > This seems the same issue as in 'Re: boiler plate class generation, writing > fresh variables with macros' to me but in a slightly different context > >> Syntax transformations in Scheme work from the outside to the inside, not the >> other way around, so you can't do things like this (define-class doesn't know >> what to do with this 'slot-machine' thing, it will reject it for not being a >> valid slot definition). However, you can define a syntax that generates the >> surrounding define-class and interprets things to insert the result of >> slot-matchine into a proper define-class form. And this explains the order of expansion! Something I've already been wondering about, what the order is or what the rules are. > I consider a (in your case recursive, but in that case more like something > like syntax-map (which can be defined recursively in terms of syntax-case)) > syntax-case to be practical for this (more so than pure syntax-rules), see my > other response. I aimed to do everything with syntax-rules, as the simplest means, but when writing the code I have, I hit the snag, that one could not have multiple ellipses at the same level of nesting in the patterns. After some thinking I found the solution to build up a temporary list, which then is of course 1 deeper level of nesting, where I could then use ellipses again. I felt quite clever doing that trick. Maybe I could implement a syntax-map using that trick and then use syntax-map in my macro instead. I have a question regarding syntax-case: If I use it, does my code become less portable to other Schemes? And regarding syntax-rules: How portable are macros, which exclusively use syntax-rules? > > Greetings, > Maxime Thank you again for your help and explanations! Things are much clearer now! Best regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl