From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Aemon Newsgroups: gmane.emacs.help Subject: Re: question on elisp best practive Date: Fri, 12 Oct 2007 00:51:08 -0000 Organization: http://groups.google.com Message-ID: <1192150268.910529.199410@v23g2000prn.googlegroups.com> References: <1191953276.499830.12710@22g2000hsm.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: sea.gmane.org 1192153252 14574 80.91.229.12 (12 Oct 2007 01:40:52 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Fri, 12 Oct 2007 01:40:52 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Oct 12 03:40:41 2007 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Ig9W3-0003oq-5d for geh-help-gnu-emacs@m.gmane.org; Fri, 12 Oct 2007 03:40:39 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ig9Vw-0002V4-TG for geh-help-gnu-emacs@m.gmane.org; Thu, 11 Oct 2007 21:40:32 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!postnews.google.com!v23g2000prn.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 50 Original-NNTP-Posting-Host: 216.57.20.61 Original-X-Trace: posting.google.com 1192150269 12008 127.0.0.1 (12 Oct 2007 00:51:09 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 12 Oct 2007 00:51:09 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: v23g2000prn.googlegroups.com; posting-host=216.57.20.61; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Original-Xref: shelby.stanford.edu gnu.emacs.help:152853 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:48346 Archived-At: On Oct 10, 2:22 am, Barry Margolin wrote: > In article <1191953276.499830.12...@22g2000hsm.googlegroups.com>, > > > > Aemon wrote: > > Hi all, > > > I've got a lot of code that traverses trees and does something at > > every node. I'd like to abstract the traversal part of the code, and > > just pass in the bit that does the action. Something like: > > > (defun visit-each (tree func depth) > > (funcall func tree depth) > > (dolist (ea (tree-children tree)) > > (visit-each ea (+ 1 depth)))) > > > This seems to work pretty well, called like so: > > > (visit-each my-tree > > (lambda (subtree depth) > > (message "%s %s" subtree depth))) > > > However, as it's not a closure I'm passing in, I would get into > > trouble if I tried: > > > (visit-each my-tree > > (let ((depth "my depth")) > > (lambda (subtree) > > (message "%s %s" subtree depth)))) > > > Elisp's dynamic scope would cause my local binding of 'depth' -> "my > > depth" to always be shadowed. > > Does it work if you use lexical-let instead of let? > > -- > Barry Margolin, bar...@alum.mit.edu > Arlington, MA > *** PLEASE post questions in newsgroups, not directly to me *** > *** PLEASE don't copy me on replies, I'll read them in the group *** Wow - I was not aware of that :) That seems to work pretty well. I guess it's probably well understood that lambdas passed to library functions should lexical-let ( or be careful what variables they reference) ? Thanks! Aemon