From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: Re: Understanding the "let" construct and the setting of variables Date: Thu, 17 Dec 2020 07:34:51 +0300 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2465"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mutt/2.0 (3d08634) (2020-11-07) Cc: Help Gnu Emacs To: steve-humphreys@gmx.com Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 17 05:45:50 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kplAk-0000Xv-4S for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 17 Dec 2020 05:45:50 +0100 Original-Received: from localhost ([::1]:35190 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kplAi-0002M9-TX for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 16 Dec 2020 23:45:48 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34770) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpl9w-0002LB-5f for help-gnu-emacs@gnu.org; Wed, 16 Dec 2020 23:45:00 -0500 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:52821) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpl9u-0008Fb-Iv for help-gnu-emacs@gnu.org; Wed, 16 Dec 2020 23:44:59 -0500 Original-Received: from localhost ([::ffff:197.157.0.58]) (AUTH: PLAIN securesender, TLS: TLS1.2,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 00000000000308FB.000000005FDAE247.0000359D; Wed, 16 Dec 2020 21:44:55 -0700 Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=217.170.207.13; envelope-from=bugs@gnu.support; helo=stw1.rcdrun.com X-Spam_score_int: -3 X-Spam_score: -0.4 X-Spam_bar: / X-Spam_report: (-0.4 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_SORBS_WEB=1.5, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:126445 Archived-At: > -*- lexical-binding: t; -*- * 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