* local binding, too local... @ 2018-12-08 2:18 Jean-Christophe Helary 2018-12-08 6:07 ` Drew Adams ` (2 more replies) 0 siblings, 3 replies; 14+ messages in thread From: Jean-Christophe Helary @ 2018-12-08 2:18 UTC (permalink / raw) To: help-gnu-emacs In a defun I wrote I have a let* block that does something, then some setf code, then a let block that does something on the values created by setf, but the let block needs a value that it set in the let* block. I would like to keep things local, but not *that* local, just *defun* local. Is there a clean way to declare variables local to a defun and without being locked by let\*? blocks ? Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune ^ permalink raw reply [flat|nested] 14+ messages in thread
* RE: local binding, too local... 2018-12-08 2:18 local binding, too local Jean-Christophe Helary @ 2018-12-08 6:07 ` Drew Adams 2018-12-08 7:39 ` Jean-Christophe Helary 2018-12-08 6:48 ` Yuri Khan [not found] ` <mailman.5363.1544251700.1284.help-gnu-emacs@gnu.org> 2 siblings, 1 reply; 14+ messages in thread From: Drew Adams @ 2018-12-08 6:07 UTC (permalink / raw) To: Jean-Christophe Helary, help-gnu-emacs > In a defun I wrote I have a let* block that does something, then some > setf code, then a let block that does something on the values created > by setf, but the let block needs a value that it set in the let* block. > > I would like to keep things local, but not *that* local, just *defun* > local. > > Is there a clean way to declare variables local to a defun and without > being locked by let\*? blocks ? How about an example? It's not clear to me what you are describing. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 6:07 ` Drew Adams @ 2018-12-08 7:39 ` Jean-Christophe Helary 0 siblings, 0 replies; 14+ messages in thread From: Jean-Christophe Helary @ 2018-12-08 7:39 UTC (permalink / raw) To: help-gnu-emacs > On Dec 8, 2018, at 15:07, Drew Adams <drew.adams@oracle.com> wrote: > >> In a defun I wrote I have a let* block that does something, then some >> setf code, then a let block that does something on the values created >> by setf, but the let block needs a value that it set in the let* block. >> >> I would like to keep things local, but not *that* local, just *defun* >> local. >> >> Is there a clean way to declare variables local to a defun and without >> being locked by let\*? blocks ? > > How about an example? It's not clear to me what > you are describing. Sorry I was going to reply but that's exactly what Yuri described and the solution I adopted is exactly what he suggested. Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 2:18 local binding, too local Jean-Christophe Helary 2018-12-08 6:07 ` Drew Adams @ 2018-12-08 6:48 ` Yuri Khan 2018-12-08 7:37 ` Jean-Christophe Helary [not found] ` <mailman.5363.1544251700.1284.help-gnu-emacs@gnu.org> 2 siblings, 1 reply; 14+ messages in thread From: Yuri Khan @ 2018-12-08 6:48 UTC (permalink / raw) To: brandelune; +Cc: help-gnu-emacs On Sat, Dec 8, 2018 at 9:19 AM Jean-Christophe Helary <brandelune@gmail.com> wrote: > In a defun I wrote I have a let* block that does something, then some setf code, then a let block that does something on the values created by setf, but the let block needs a value that it set in the let* block. The customary way is to put the consumer block within the producer block: (defun foo () (let* ((bar '(baz quux))) (setf (car bar) 'xyzzy) (let ((plugh (cdr bar))) (message "%s" plugh)))) > I would like to keep things local, but not *that* local, just *defun* local. > > Is there a clean way to declare variables local to a defun and without being locked by let\*? blocks ? let and let* are *binding* forms, not *assignment* statements. The bindings go in scope, the body is executed, the bindings go out of scope. setq, on the other hand, is an assignment form. If a named variable exists in the current scope, it will reset its value. However, the longer a variable is and the broader its scope is, the more difficult it is to track where and how it it modified. ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 6:48 ` Yuri Khan @ 2018-12-08 7:37 ` Jean-Christophe Helary 2018-12-08 8:02 ` tomas 0 siblings, 1 reply; 14+ messages in thread From: Jean-Christophe Helary @ 2018-12-08 7:37 UTC (permalink / raw) To: help-gnu-emacs > On Dec 8, 2018, at 15:48, Yuri Khan <yurivkhan@gmail.com> wrote: > > On Sat, Dec 8, 2018 at 9:19 AM Jean-Christophe Helary > <brandelune@gmail.com> wrote: > >> In a defun I wrote I have a let* block that does something, then some setf code, then a let block that does something on the values created by setf, but the let block needs a value that it set in the let* block. > > The customary way is to put the consumer block within the producer block: > > (defun foo () > (let* ((bar '(baz quux))) > (setf (car bar) 'xyzzy) > (let ((plugh (cdr bar))) > (message "%s" plugh)))) Eventually that's what I did. But I was not sure if that would be considered a "hack" or is the correct way to think about things. >> I would like to keep things local, but not *that* local, just *defun* local. >> >> Is there a clean way to declare variables local to a defun and without being locked by let\*? blocks ? > > let and let* are *binding* forms, not *assignment* statements. The > bindings go in scope, the body is executed, the bindings go out of > scope. > > setq, on the other hand, is an assignment form. If a named variable > exists in the current scope, it will reset its value. I'm not sure I understand the difference yet between binding and assigning yet. Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 7:37 ` Jean-Christophe Helary @ 2018-12-08 8:02 ` tomas 2018-12-08 14:45 ` Jean-Christophe Helary 0 siblings, 1 reply; 14+ messages in thread From: tomas @ 2018-12-08 8:02 UTC (permalink / raw) To: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 878 bytes --] On Sat, Dec 08, 2018 at 04:37:59PM +0900, Jean-Christophe Helary wrote: [...] > I'm not sure I understand the difference yet between binding and assigning yet. Binding: you associate a name (strictly: a symbol) to a place where to store something (a "variable"). Assigning: you change the content (the thing stored in) an existing variable. Often you get those two steps in one package, and that's why the difference isn't so clear. Consider: (let ((foo 12)) ; bind a variable to the symbol foo ... (... ; some sub-scope, e.g. another let ; let's assume it does *not* bind foo (setq foo 13) ; Now the foo is bound to the same variable, ; but that contains now 13 )) Things are a bit confusing, because (let ((foo 12)) ...) binds foo *and* assigns 12. But you could just use (let (foo) ...) in its pure form Cheers -- t [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 8:02 ` tomas @ 2018-12-08 14:45 ` Jean-Christophe Helary 2018-12-08 21:59 ` tomas 0 siblings, 1 reply; 14+ messages in thread From: Jean-Christophe Helary @ 2018-12-08 14:45 UTC (permalink / raw) To: help-gnu-emacs Thank you Tomas. So can you say that "binding" is like "creating" a variable ? While assigning is just putting a value in that variable ? I remember in my math classes, when we were using x and y, etc. we were binding x and y, right ? And then when we had values for x and y we were assigning values. Is that correct ? Jean-Christophe > On Dec 8, 2018, at 17:02, <tomas@tuxteam.de> <tomas@tuxteam.de> wrote: > > On Sat, Dec 08, 2018 at 04:37:59PM +0900, Jean-Christophe Helary wrote: > > [...] > >> I'm not sure I understand the difference yet between binding and assigning yet. > > Binding: you associate a name (strictly: a symbol) to a place where to > store something (a "variable"). > > Assigning: you change the content (the thing stored in) an existing variable. > > Often you get those two steps in one package, and that's why the difference > isn't so clear. > > Consider: > > > (let ((foo 12)) ; bind a variable to the symbol foo > ... > (... ; some sub-scope, e.g. another let > ; let's assume it does *not* bind foo > (setq foo 13) ; Now the foo is bound to the same variable, > ; but that contains now 13 > )) > > Things are a bit confusing, because (let ((foo 12)) ...) binds foo *and* > assigns 12. But you could just use (let (foo) ...) in its pure form > > Cheers > -- t Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 14:45 ` Jean-Christophe Helary @ 2018-12-08 21:59 ` tomas 2018-12-09 2:41 ` Jean-Christophe Helary 0 siblings, 1 reply; 14+ messages in thread From: tomas @ 2018-12-08 21:59 UTC (permalink / raw) To: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 897 bytes --] On Sat, Dec 08, 2018 at 11:45:32PM +0900, Jean-Christophe Helary wrote: > Thank you Tomas. > > So can you say that "binding" is like "creating" a variable ? No (at least not in this Lisp context). It's more like associating a name with a variable. > While assigning is just putting a value in that variable ? Kind of, yes. Reality is a bit more complex, though... > I remember in my math classes, when we were using x and y, etc. we were binding x and y, right ? And then when we had values for x and y we were assigning values. Is that correct ? In maths, binding means associating a value with a name, that's easy. And there's no correspondence for assignment. You can substitute a bound variable (say x is bound to 5) by for its value everywhere. "Assigning 7 to x" wouldn't make much sense in maths, because it would amount to assigning 7 to 5. Cheers -- tomás [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 21:59 ` tomas @ 2018-12-09 2:41 ` Jean-Christophe Helary 2018-12-09 8:45 ` tomas 0 siblings, 1 reply; 14+ messages in thread From: Jean-Christophe Helary @ 2018-12-09 2:41 UTC (permalink / raw) To: help-gnu-emacs > On Dec 9, 2018, at 6:59, <tomas@tuxteam.de> <tomas@tuxteam.de> wrote: > > On Sat, Dec 08, 2018 at 11:45:32PM +0900, Jean-Christophe Helary wrote: >> Thank you Tomas. >> >> So can you say that "binding" is like "creating" a variable ? > > No (at least not in this Lisp context). It's more like associating > a name with a variable. But there must be something that's created that did not exist before that "binding" right ? >> While assigning is just putting a value in that variable ? > > Kind of, yes. Reality is a bit more complex, though... > >> I remember in my math classes, when we were using x and y, etc. we were binding x and y, right ? And then when we had values for x and y we were assigning values. Is that correct ? > > In maths, binding means associating a value with a name, that's easy. > And there's no correspondence for assignment. You can substitute a > bound variable (say x is bound to 5) by for its value everywhere. > > "Assigning 7 to x" wouldn't make much sense in maths, because it would > amount to assigning 7 to 5. Ok so the comparison with maths doesn't work, right ? Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-09 2:41 ` Jean-Christophe Helary @ 2018-12-09 8:45 ` tomas 2018-12-09 9:55 ` Jean-Christophe Helary 0 siblings, 1 reply; 14+ messages in thread From: tomas @ 2018-12-09 8:45 UTC (permalink / raw) To: Jean-Christophe Helary; +Cc: help-gnu-emacs [-- Attachment #1: Type: text/plain, Size: 1197 bytes --] On Sun, Dec 09, 2018 at 11:41:19AM +0900, Jean-Christophe Helary wrote: > > > > On Dec 9, 2018, at 6:59, <tomas@tuxteam.de> <tomas@tuxteam.de> wrote: > > > > On Sat, Dec 08, 2018 at 11:45:32PM +0900, Jean-Christophe Helary wrote: > >> Thank you Tomas. > >> > >> So can you say that "binding" is like "creating" a variable ? > > > > No (at least not in this Lisp context). It's more like associating > > a name with a variable. > > But there must be something that's created that did not exist before that "binding" right ? The association :-) Technically, there's some hash table in the background, where the implementation can look up symbols and what's been "hung" on them (besides a "variable binding" there are some other possibilities,,,) > > "Assigning 7 to x" wouldn't make much sense in maths, because it would > > amount to assigning 7 to 5. > > Ok so the comparison with maths doesn't work, right ? I'd say that the "maths" view was an inspiration, so it always helps to keep it around for comparison. But it's important to remember that there are differences. That doesn't mean that maths can't model assignment, though. Cheers -- tomás [-- Attachment #2: Digital signature --] [-- Type: application/pgp-signature, Size: 198 bytes --] ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-09 8:45 ` tomas @ 2018-12-09 9:55 ` Jean-Christophe Helary 0 siblings, 0 replies; 14+ messages in thread From: Jean-Christophe Helary @ 2018-12-09 9:55 UTC (permalink / raw) To: help-gnu-emacs > On Dec 9, 2018, at 17:45, <tomas@tuxteam.de> <tomas@tuxteam.de> wrote: > > On Sun, Dec 09, 2018 at 11:41:19AM +0900, Jean-Christophe Helary wrote: >> >> >>> On Dec 9, 2018, at 6:59, <tomas@tuxteam.de> <tomas@tuxteam.de> wrote: >>> >>> On Sat, Dec 08, 2018 at 11:45:32PM +0900, Jean-Christophe Helary wrote: >>>> Thank you Tomas. >>>> >>>> So can you say that "binding" is like "creating" a variable ? >>> >>> No (at least not in this Lisp context). It's more like associating >>> a name with a variable. >> >> But there must be something that's created that did not exist before that "binding" right ? > > The association :-) Ok, that's what I needed to know :) > Technically, there's some hash table in the background, where > the implementation can look up symbols and what's been "hung" > on them (besides a "variable binding" there are some other > possibilities,,,) Thank you. > >>> "Assigning 7 to x" wouldn't make much sense in maths, because it would >>> amount to assigning 7 to 5. >> >> Ok so the comparison with maths doesn't work, right ? > > I'd say that the "maths" view was an inspiration, so it always helps > to keep it around for comparison. But it's important to remember that > there are differences. > > That doesn't mean that maths can't model assignment, though. That will be for another time :) Jean-Christophe Helary ----------------------------------------------- http://mac4translators.blogspot.com @brandelune ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.5363.1544251700.1284.help-gnu-emacs@gnu.org>]
* Re: local binding, too local... [not found] ` <mailman.5363.1544251700.1284.help-gnu-emacs@gnu.org> @ 2018-12-08 9:33 ` Rusi 2018-12-08 16:41 ` Stefan Monnier [not found] ` <mailman.5389.1544287285.1284.help-gnu-emacs@gnu.org> 0 siblings, 2 replies; 14+ messages in thread From: Rusi @ 2018-12-08 9:33 UTC (permalink / raw) To: help-gnu-emacs Yuri wrote: (let* ((bar '(baz quux))) (setf (car bar) 'xyzzy) (let ((plugh (cdr bar))) (message "%s" plugh)))) Can also be written (let (bar plugh) (setf bar '(baz quux)) (setf (car bar) 'xyzzy) (setf plugh (cdr bar))) IOW declare variables without initializers. Then write 'normal'imperative code ^ permalink raw reply [flat|nested] 14+ messages in thread
* Re: local binding, too local... 2018-12-08 9:33 ` Rusi @ 2018-12-08 16:41 ` Stefan Monnier [not found] ` <mailman.5389.1544287285.1284.help-gnu-emacs@gnu.org> 1 sibling, 0 replies; 14+ messages in thread From: Stefan Monnier @ 2018-12-08 16:41 UTC (permalink / raw) To: help-gnu-emacs > (let* ((bar '(baz quux))) > (setf (car bar) 'xyzzy) > (let ((plugh (cdr bar))) > (message "%s" plugh)))) > > Can also be written > > (let (bar plugh) > (setf bar '(baz quux)) > (setf (car bar) 'xyzzy) > (setf plugh (cdr bar))) Indeed, but you lose a lot of karma points for that, Stefan ^ permalink raw reply [flat|nested] 14+ messages in thread
[parent not found: <mailman.5389.1544287285.1284.help-gnu-emacs@gnu.org>]
* Re: local binding, too local... [not found] ` <mailman.5389.1544287285.1284.help-gnu-emacs@gnu.org> @ 2018-12-08 19:13 ` Barry Fishman 0 siblings, 0 replies; 14+ messages in thread From: Barry Fishman @ 2018-12-08 19:13 UTC (permalink / raw) To: help-gnu-emacs On 2018-12-08 11:41:10 -05, Stefan Monnier wrote: >> (let* ((bar '(baz quux))) >> (setf (car bar) 'xyzzy) >> (let ((plugh (cdr bar))) >> (message "%s" plugh)))) >> >> Can also be written >> >> (let (bar plugh) >> (setf bar '(baz quux)) >> (setf (car bar) 'xyzzy) >> (setf plugh (cdr bar))) > > Indeed, but you lose a lot of karma points for that, Haven't you already lost all your karma points by modifying a literal list no matter how you do it. ;-) -- Barry Fishman ^ permalink raw reply [flat|nested] 14+ messages in thread
end of thread, other threads:[~2018-12-09 9:55 UTC | newest] Thread overview: 14+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2018-12-08 2:18 local binding, too local Jean-Christophe Helary 2018-12-08 6:07 ` Drew Adams 2018-12-08 7:39 ` Jean-Christophe Helary 2018-12-08 6:48 ` Yuri Khan 2018-12-08 7:37 ` Jean-Christophe Helary 2018-12-08 8:02 ` tomas 2018-12-08 14:45 ` Jean-Christophe Helary 2018-12-08 21:59 ` tomas 2018-12-09 2:41 ` Jean-Christophe Helary 2018-12-09 8:45 ` tomas 2018-12-09 9:55 ` Jean-Christophe Helary [not found] ` <mailman.5363.1544251700.1284.help-gnu-emacs@gnu.org> 2018-12-08 9:33 ` Rusi 2018-12-08 16:41 ` Stefan Monnier [not found] ` <mailman.5389.1544287285.1284.help-gnu-emacs@gnu.org> 2018-12-08 19:13 ` Barry Fishman
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).