all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Can free variable refers to a lexical environment?
@ 2010-03-09 12:43 IWAKI Hidekazu
  2010-03-09 18:21 ` Barry Margolin
  0 siblings, 1 reply; 3+ messages in thread
From: IWAKI Hidekazu @ 2010-03-09 12:43 UTC (permalink / raw
  To: help-gnu-emacs

Hello,

I need to make a table factory function in emacs with cl extension.
but I'm scheme user. I'm confusing emacs lisp behavior.
will you please tell me the emacs lisp's free variable issue.

my table factory function is following code:
 (defun mk-table-instance ()
  (let ((table nil)) ;; create a lexical value `table`
    (defun __temp__ (msg &rest value)
      ;; some operation changes the lexical variable `table`
      (case msg
	((push) (push (car value) table))
	(otherwise table)))
    (function __temp__)))
;; return the `__temp__`  procedure with the lexical variable `table`
;; i.e. return a closure.

(fset 'table-object (mk-table-instance))
;; 'table-object is an unique procedure object.
(table-object 'push 13)
;; my plan => operate an unique `table` variable which was created by
involving `mk-table-instance`
;; real        => "Debugger entered--Lisp error: (void-variable
table)"!!!!!!!

I researched this code. I guess the lexical binded variable `table` in
`mk-table-instance` procedure refer to the global environment.
In scheme, the variable refer to the lexical environment.

How to refer to a lexical environment?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Can free variable refers to a lexical environment?
  2010-03-09 12:43 Can free variable refers to a lexical environment? IWAKI Hidekazu
@ 2010-03-09 18:21 ` Barry Margolin
  2010-03-10  4:47   ` IWAKI Hidekazu
  0 siblings, 1 reply; 3+ messages in thread
From: Barry Margolin @ 2010-03-09 18:21 UTC (permalink / raw
  To: help-gnu-emacs

In article 
<180666d9-2ecb-4d99-a419-739650a8fb3d@c37g2000prb.googlegroups.com>,
 IWAKI Hidekazu <i.hidekazu@gmail.com> wrote:

> Hello,
> 
> I need to make a table factory function in emacs with cl extension.
> but I'm scheme user. I'm confusing emacs lisp behavior.
> will you please tell me the emacs lisp's free variable issue.
> 
> my table factory function is following code:
>  (defun mk-table-instance ()
>   (let ((table nil)) ;; create a lexical value `table`
>     (defun __temp__ (msg &rest value)
>       ;; some operation changes the lexical variable `table`
>       (case msg
> 	((push) (push (car value) table))
> 	(otherwise table)))
>     (function __temp__)))
> ;; return the `__temp__`  procedure with the lexical variable `table`
> ;; i.e. return a closure.
> 
> (fset 'table-object (mk-table-instance))
> ;; 'table-object is an unique procedure object.
> (table-object 'push 13)
> ;; my plan => operate an unique `table` variable which was created by
> involving `mk-table-instance`
> ;; real        => "Debugger entered--Lisp error: (void-variable
> table)"!!!!!!!
> 
> I researched this code. I guess the lexical binded variable `table` in
> `mk-table-instance` procedure refer to the global environment.
> In scheme, the variable refer to the lexical environment.
> 
> How to refer to a lexical environment?

Emacs Lisp implements dynamic scoping, not lexical scoping.

You can use lexical-let to emulate lexical binding.

-- 
Barry Margolin, barmar@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 ***


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Can free variable refers to a lexical environment?
  2010-03-09 18:21 ` Barry Margolin
@ 2010-03-10  4:47   ` IWAKI Hidekazu
  0 siblings, 0 replies; 3+ messages in thread
From: IWAKI Hidekazu @ 2010-03-10  4:47 UTC (permalink / raw
  To: help-gnu-emacs

On Mar 10, 3:21 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article
> <180666d9-2ecb-4d99-a419-739650a8f...@c37g2000prb.googlegroups.com>,
>  IWAKI Hidekazu <i.hidek...@gmail.com> wrote:
>
>
>
>
>
> > Hello,
>
> > I need to make a table factory function in emacs with cl extension.
> > but I'm scheme user. I'm confusing emacs lisp behavior.
> > will you please tell me the emacs lisp's free variable issue.
>
> > my table factory function is following code:
> >  (defun mk-table-instance ()
> >   (let ((table nil)) ;; create a lexical value `table`
> >     (defun __temp__ (msg &rest value)
> >       ;; some operation changes the lexical variable `table`
> >       (case msg
> >    ((push) (push (car value) table))
> >    (otherwise table)))
> >     (function __temp__)))
> > ;; return the `__temp__`  procedure with the lexical variable `table`
> > ;; i.e. return a closure.
>
> > (fset 'table-object (mk-table-instance))
> > ;; 'table-object is an unique procedure object.
> > (table-object 'push 13)
> > ;; my plan => operate an unique `table` variable which was created by
> > involving `mk-table-instance`
> > ;; real        => "Debugger entered--Lisp error: (void-variable
> > table)"!!!!!!!
>
> > I researched this code. I guess the lexical binded variable `table` in
> > `mk-table-instance` procedure refer to the global environment.
> > In scheme, the variable refer to the lexical environment.
>
> > How to refer to a lexical environment?
>
> Emacs Lisp implements dynamic scoping, not lexical scoping.
>
> You can use lexical-let to emulate lexical binding.
>
> --
> 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 ***

Oh, it's just nice. I can use lexical binding in elisp programs.

I've tried some, this macro can not solve labeled procedures("defun"ed
procedures),
but only no labeled procedures (lambda expressions). I'll rewrite my
programs in the match style.

thank you for your advice.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-03-10  4:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-03-09 12:43 Can free variable refers to a lexical environment? IWAKI Hidekazu
2010-03-09 18:21 ` Barry Margolin
2010-03-10  4:47   ` IWAKI Hidekazu

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.