unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: steve-humphreys@gmx.com
To: Jean Louis <bugs@gnu.support>
Cc: Help Gnu Emacs <help-gnu-emacs@gnu.org>
Subject: Re: Understanding the "let" construct and the setting of variables
Date: Thu, 17 Dec 2020 06:12:00 +0100	[thread overview]
Message-ID: <trinity-7b78191b-0c53-44c8-9232-76059a22ff63-1608181920893@3c-app-mailcom-bs11> (raw)
In-Reply-To: <X9rf64FEKYsB4Oa1@protected.rcdrun.com>

> How I understand it is that `setq' I can freely use on variables
> already defined with and within my `let' as then the variable
> will not become global.
> 
> (defun my-fun ()
>   (let ((my-var nil))
>     (setq my-var 2)))
> 
> (my-fun)
> 
> my-var is not defined
Here the variable remains local.  my-var does not become a global
variable because it is defined within the let.  So one can use "set"
with variables defined in a "let" construct.  I thought you could not
do that - call "setq" on a local variable defined in a "let" expression.


 
> (defun my-fun ()
>   (let ((my-var nil)))
>   (setq my-var 2))
> 
> (my-fun)
> 
> my-var is here defined as 2 and became global variable.
> 
> And each time that variable is already defined with `defvar' one
> can then change it with setq.
And here "my-var" becames a global variable because my-var is
set using "setq" outside the "let" expression.

Thanks.  Was not too difficult as I thought.  



> Sent: Thursday, December 17, 2020 at 5:34 AM
> From: "Jean Louis" <bugs@gnu.support>
> To: steve-humphreys@gmx.com
> Cc: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Re: Understanding the "let" construct and the setting of variables
>
> > -*- lexical-binding: t; -*-
> * steve-humphreys@gmx.com <steve-humphreys@gmx.com> [2020-12-17 03:26]:
> > Let's introspect two questions.
> > 
> > 1. In what simple circumstances would one use a "setq" in the body
> > of a let?
> 
> Whenever I find myself in linear programming within a function and
> need to change variable I will use setq. Some global variables are
> rather set with setq:
> 
>     (set-buffer buffer)
>     (setq header-line-format (concat buffer " ➜ Finish with `q' or `h'"))
>     (cf-org-view-mode)
>     (insert blob)
>     (setq org-hierarchical-todo-statistics nil)
>     (org-update-parent-todo-statistics)
>     (goto-char 1)
> 
> But I will often use it in construction of lists:
> 
> (defun rcd-cgi-parse-query-string (query-string)
>   "Parse QUERY-STRING that normally comes from the environment
> variable `QUERY_STRING'. Return PLIST."
>   (let* ((query-string (url-unhex-string query-string))
> 	 (parts (split-string query-string "&"))
> 	 (length (length parts))
> 	 (plist '()))
>     (dolist (part parts plist)
>       (let* ((data (split-string part "="))
> 	     (prop (car data))
> 	     (val (cadr data)))
> 	(setq plist (plist-put plist (intern prop) val))))))
> 
> 
> (defun iota (count &optional start step)
>   "Return a list containing COUNT numbers, starting from START
> and adding STEP each time.  The default START is 0, the default
> STEP is 1"
>   (let* ((start (if start start 0))
> 	 (step (if step step 1))
> 	 (last (+ start count))
> 	 (counter 0)
> 	 (list '())
> 	 (elt start))
>     (while (< counter count)
>       (push elt list)
>       (setq elt (+ elt step))
>       (setq counter (1+ counter)))
>     (reverse list)))
> 
> How I understand it is that `setq' I can freely use on variables
> already defined with and within my `let' as then the variable
> will not become global.
> 
> (defun my-fun ()
>   (let ((my-var nil))
>     (setq my-var 2)))
> 
> (my-fun)
> 
> my-var is not defined
> 
> (defun my-fun ()
>   (let ((my-var nil)))
>   (setq my-var 2))
> 
> (my-fun)
> 
> my-var is here defined as 2 and became global variable.
> 
> And each time that variable is already defined with `defvar' one
> can then change it with setq.
> 
> Jean
>



  reply	other threads:[~2020-12-17  5:12 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-17  0:10 Understanding the "let" construct and the setting of variables steve-humphreys
2020-12-17  0:21 ` Joost Kremers
2020-12-17  2:08   ` steve-humphreys
2020-12-17  3:12     ` steve-humphreys
2020-12-17  8:01       ` Joost Kremers
2020-12-17  8:31         ` steve-humphreys
2020-12-17  8:50           ` Joost Kremers
2020-12-17  8:10     ` Joost Kremers
2020-12-17  8:43       ` steve-humphreys
2020-12-17  8:56         ` Joost Kremers
2020-12-18 20:48           ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 20:46         ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 21:07           ` Jean Louis
2020-12-18 22:31           ` tomas
2020-12-18 20:39       ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-17  2:49   ` steve-humphreys
2020-12-17  7:58     ` Joost Kremers
2020-12-17 16:55       ` Drew Adams
2020-12-17 20:11         ` steve-humphreys
2020-12-17 21:57           ` Drew Adams
2020-12-17 22:35           ` Michael Heerdegen
2020-12-18  9:01             ` tomas
2020-12-18  9:16               ` Michael Heerdegen
2020-12-18 20:55                 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-19  2:17                   ` Michael Heerdegen
2020-12-19  2:52                     ` Drew Adams
2020-12-19  5:15                     ` Stefan Monnier
2020-12-18 20:33   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-17  0:25 ` steve-humphreys
2020-12-17  0:35   ` steve-humphreys
2020-12-17  1:05     ` Joost Kremers
2020-12-17  1:20       ` steve-humphreys
2020-12-18 20:58         ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-17  4:34   ` Jean Louis
2020-12-17  5:12     ` steve-humphreys [this message]
2020-12-19  6:06       ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-17  7:31     ` steve-humphreys
2020-12-19  5:55     ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-19  6:49       ` Jean Louis
2020-12-20  5:19         ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 17:14   ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 17:48     ` tomas
2020-12-18 15:33 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 18:12   ` Jean Louis
2020-12-18 18:20     ` Drew Adams
2020-12-18 18:45       ` Jean Louis
2020-12-18 19:16         ` Drew Adams
2020-12-18 20:00           ` Jean Louis
2020-12-18 21:27     ` Christopher Dimech
2020-12-19  6:23     ` Emanuel Berg via Users list for the GNU Emacs text editor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=trinity-7b78191b-0c53-44c8-9232-76059a22ff63-1608181920893@3c-app-mailcom-bs11 \
    --to=steve-humphreys@gmx.com \
    --cc=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).