From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Price Newsgroups: gmane.lisp.guile.user Subject: Re: Syntax-rules generate symbol Date: Tue, 10 Sep 2013 11:33:41 +0100 Message-ID: <878uz5neyi.fsf@Kagami.home> References: <87txhuarxf.fsf@gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1378809242 19319 80.91.229.3 (10 Sep 2013 10:34:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 10 Sep 2013 10:34:02 +0000 (UTC) Cc: "guile-user@gnu.org" To: Dmitry Bogatov Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Sep 10 12:34:07 2013 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1VJLGn-0004G4-Hy for guile-user@m.gmane.org; Tue, 10 Sep 2013 12:34:05 +0200 Original-Received: from localhost ([::1]:56899 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJLGn-0003Vn-4n for guile-user@m.gmane.org; Tue, 10 Sep 2013 06:34:05 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50877) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJLGb-0003VW-EQ for guile-user@gnu.org; Tue, 10 Sep 2013 06:33:57 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VJLGU-0004GY-9k for guile-user@gnu.org; Tue, 10 Sep 2013 06:33:53 -0400 Original-Received: from mail-we0-x234.google.com ([2a00:1450:400c:c03::234]:59052) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VJLGU-0004Fx-3Y; Tue, 10 Sep 2013 06:33:46 -0400 Original-Received: by mail-we0-f180.google.com with SMTP id u57so5272912wes.25 for ; Tue, 10 Sep 2013 03:33:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:cc:subject:references:mail-followup-to:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=z7jV/x+b29IWDrpPmoPMmC5Ow2xMN39q+O+2BfQ2xQM=; b=0EMzOatHlTuL6ylv8jM0d12Sky/lH4SMyujD0D8QL2fHouqueYVdLcZtaS4TEAdF3I Y5bNi7wKx7XfnVh71Zj3Lu50wPrjlxbLQE1hkFSRxx+pJ2hsKJUXpYpGp1/QN5YUt5U1 lQKxreG3dp/ZIQQ6nPZz8NUXHEi+epWFu6RZkXkDnlz1hqapkB1Wvwp/wu14VQZU/CSb Mshi+fRppHN+3AYvKPfn4tLOge8Vxsz67Cd2I34AZYLJ3o7kDsP12vGT53s8ix7nd28x K+vj0LJXBgKYuwVRxJqSiZaw3KsoqSgYBKAzJuNcg8hWgmIS/e67dW8nHGV+1BU4Z+H+ /c8w== X-Received: by 10.180.38.73 with SMTP id e9mr9846323wik.31.1378809225041; Tue, 10 Sep 2013 03:33:45 -0700 (PDT) Original-Received: from Kagami.home (host109-157-170-145.range109-157.btcentralplus.com. [109.157.170.145]) by mx.google.com with ESMTPSA id i3sm2533864wiw.7.1969.12.31.16.00.00 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Tue, 10 Sep 2013 03:33:44 -0700 (PDT) Mail-Followup-To: Dmitry Bogatov , "guile-user\@gnu.org" In-Reply-To: <87txhuarxf.fsf@gnu.org> (Dmitry Bogatov's message of "Mon, 09 Sep 2013 20:19:40 +0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.1 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c03::234 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 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-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:10788 Archived-At: Dmitry Bogatov writes: > Hello! > > Here is my implementation of for loop. I found lisp really extremely > flexible, but there is one problem --- more often then not I do not need > var part, so I do not care how it would be named --- all I care is that > it will not shadow any other bindings. > > I think I can do it(did not tryed it) with `define-macro` and uninterned > symbols, but it mean give up beauty of syntax-rules. > > Masters of syntax-rules and syntax-case, please give me peace of advice. > > (define-syntax for > (syntax-rules (in => as) > ([_ (pattern as var in list) exp ...] > [for-each (lambda (var) (match var (pattern exp ...))) list]))) Actually, it couldn't be simpler, just add an extra pattern that passes in a dummy name. No (explicit) gensym needed. (define-syntax for (syntax-rules (in => as) [(_ (pattern as var in list) exp ...) (for-each (lambda (var) (match var (pattern exp ...))) list)] [(_ (pattern in list) exp ...) (for (pattern as var in list) exp ...)])) Now we get scheme@(guile-user)> (for (a in '(1 2 3)) (pk a)) ;;; (1) ;;; (2) ;;; (3) scheme@(guile-user)> (for (a in '(1 2 3)) (pk var)) ;;; :52:21: warning: possibly unbound variable `var' :52:0: In procedure #:52:0 (var)>: :52:0: In procedure module-lookup: Unbound variable: var Entering a new prompt. Type `,bt' for a backtrace or `,q' to continue. ,expand (for (a in '(1 2 3)) (pk var)) $4 = (for-each (lambda (var-1) (let* ((v var-1) (failure (lambda () (((@@ (ice-9 match) error) 'match "no matching pattern" v)))) (a v)) (pk var))) '(1 2 3)) Var is unbound as expected. Though this is not obvious if you use ,expand on an example that doesn't use 'var', since Guile tries to clean up the names for readability. The reason it's okay to just pass in the dummy is because of how syntax-case and syntax-rules work. If a name is not passed explicitly into the macro, and it is not a reference to a top-level function or macro, then syntax-case or syntax-rules will rename it automatically. I took the liberty of fixing up the indentation, and changing the use of [] to a more idiomatic one, but there is still one obvious issue with your macro. "exp ..." means 0 or more expressions. If you had intended one or more, as is more usual in this type of macro, you'll want to say so explicitly with something like "exp exps ...". Cheers -- Ian Price -- shift-reset.com "Programming is like pinball. The reward for doing it well is the opportunity to do it again" - from "The Wizardy Compiled"