[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: question on elisp best practive
From: |
Aemon |
Subject: |
Re: question on elisp best practive |
Date: |
Fri, 12 Oct 2007 00:51:08 -0000 |
User-agent: |
G2/1.0 |
On Oct 10, 2:22 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <1191953276.499830.12...@22g2000hsm.googlegroups.com>,
>
>
>
> Aemon <aemoncan...@gmail.com> 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