From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: steve-humphreys@gmx.com Newsgroups: gmane.emacs.help Subject: Re: Understanding the "let" construct and the setting of variables Date: Thu, 17 Dec 2020 06:12:00 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38427"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Help Gnu Emacs To: Jean Louis Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 17 06:13:04 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 1kplb5-0009uc-8c for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 17 Dec 2020 06:13:03 +0100 Original-Received: from localhost ([::1]:48638 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kplb4-00011W-A8 for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 17 Dec 2020 00:13:02 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43102) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kplaf-00011D-12 for help-gnu-emacs@gnu.org; Thu, 17 Dec 2020 00:12:37 -0500 Original-Received: from mout.gmx.net ([212.227.17.21]:60951) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kplac-0001JU-08 for help-gnu-emacs@gnu.org; Thu, 17 Dec 2020 00:12:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1608181921; bh=4aIz0B9pd3quv3ridZpMkBA3S7/hoLN3j7OTHUAfMqs=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=O2UG7cc6l5J/MPo1SYyb4iWL64oTrcVY2LIZwuGbgPT2EaqAMLEIanHj8rSq7AjXP S3pCm9rISCZv7mEitHnDhsIW6oQB2a9f55Ea2kqTnGaeZJWHqGCUrZ+B3eZhFTVFWp IeboTakGmXQqAoEQn+ssaI/riWwwmDj5x+SNnnLc= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from [213.165.168.94] ([213.165.168.94]) by web-mail.gmx.net (3c-app-mailcom-bs11.server.lan [172.19.170.179]) (via HTTP); Thu, 17 Dec 2020 06:12:00 +0100 Importance: normal Sensitivity: Normal In-Reply-To: X-UI-Message-Type: mail X-Priority: 3 X-Provags-ID: V03:K1:wNuvGjK1sRzA5h1b/ajETxq/natDO2DdgILxzHkGujEBNATlcJXP8hdX0slkb0Lg7mRsa QiiD+WKtZigsZQ9oR54HWeBjvpQhXG+7ih16dbSQqPwS3s67LB4kUVsHt+OQhDNsVtMFlk03AJLn /XOeZgx5bE6J29wc7CNz0/gtrp3VU2dhM9dpvllvHA8rhkocsePxoE8ZcS6jn2lz5YKUTP4h6yoi XM9o+AlsWs8Cuil03d+9nP4owHPL7Tz8Lc5Bc4XYjk/h4/hzU+Qnyc4lzBMrKxwkOMB5tBwhOYQo Ac= X-UI-Out-Filterresults: notjunk:1;V03:K0:WHJYDxQq7pg=:Wej87AyqUbD3bOkmosyWRm 0ZwklAoD/2jRwtBwXmgu0Ea9c9eFz+CQcuo83Mq3YlmXXvGrp/z+uea4yxz+gONLONOTJBWM1 udXY8bIQQ94cSbhjXnhO8+If1gj4utJ2qjOv11YvJIct5x6FZfTAm2TRlZrSxahE5aga6Xpk9 onJis3/ViUuIZ8mH1SNu3Qtz9ZJgafzKS28Y84IEskENHfDeKb3mD/SGGrPogCc23QA2A/xfX IRz6JRx5Ae7953bChEonYov+2B4T349+99Qntsl3UOlX/Ti+fTLfatnH8eLocZzUL4bWoIVFQ aW+EatTgu1+zZ/SljaEclsD3yad6a44Ux7i/n1hfCAb7giuVR9pTn4gvvOiP5M7RC6x9OiHrw umYwebWwMw1THyz94VbXgScJnr6r8WXkuEgJ5TwqZ178NJJvSi0fAiqVdIl39kiIaF0xsA+JI 4HM4JjhYa4AX+i6kcR4mbcaB0CJfijyblOSfXMjB5flaty0RRIc45f7PfocjFUKvvIdYz18Xk YNjxaID4BtlXF/F3PInya+NEK5eThhl/xoJx024WN5b+yofPxTv1PlTGK7oyCn0ChJlJKMVkJ nxFsjh0b6xs4I= Received-SPF: pass client-ip=212.227.17.21; envelope-from=steve-humphreys@gmx.com; helo=mout.gmx.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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:126448 Archived-At: > 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=2E >=20 > (defun my-fun () > (let ((my-var nil)) > (setq my-var 2))) >=20 > (my-fun) >=20 > my-var is not defined Here the variable remains local=2E my-var does not become a global variable because it is defined within the let=2E So one can use "set" with variables defined in a "let" construct=2E I thought you could not do that - call "setq" on a local variable defined in a "let" expression=2E =20 > (defun my-fun () > (let ((my-var nil))) > (setq my-var 2)) >=20 > (my-fun) >=20 > my-var is here defined as 2 and became global variable=2E >=20 > And each time that variable is already defined with `defvar' one > can then change it with setq=2E And here "my-var" becames a global variable because my-var is set using "setq" outside the "let" expression=2E Thanks=2E Was not too difficult as I thought=2E =20 > Sent: Thursday, December 17, 2020 at 5:34 AM > From: "Jean Louis" > To: steve-humphreys@gmx=2Ecom > Cc: "Help Gnu Emacs" > Subject: Re: Understanding the "let" construct and the setting of variab= les > > > -*- lexical-binding: t; -*- > * steve-humphreys@gmx=2Ecom [2020-12-17 03:2= 6]: > > Let's introspect two questions=2E > >=20 > > 1=2E In what simple circumstances would one use a "setq" in the body > > of a let? >=20 > Whenever I find myself in linear programming within a function and > need to change variable I will use setq=2E Some global variables are > rather set with setq: >=20 > (set-buffer buffer) > (setq header-line-format (concat buffer " =E2=9E=9C 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) >=20 > But I will often use it in construction of lists: >=20 > (defun rcd-cgi-parse-query-string (query-string) > "Parse QUERY-STRING that normally comes from the environment > variable `QUERY_STRING'=2E Return PLIST=2E" > (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 "=3D")) > (prop (car data)) > (val (cadr data))) > (setq plist (plist-put plist (intern prop) val)))))) >=20 >=20 > (defun iota (count &optional start step) > "Return a list containing COUNT numbers, starting from START > and adding STEP each time=2E 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))) >=20 > 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=2E >=20 > (defun my-fun () > (let ((my-var nil)) > (setq my-var 2))) >=20 > (my-fun) >=20 > my-var is not defined >=20 > (defun my-fun () > (let ((my-var nil))) > (setq my-var 2)) >=20 > (my-fun) >=20 > my-var is here defined as 2 and became global variable=2E >=20 > And each time that variable is already defined with `defvar' one > can then change it with setq=2E >=20 > Jean >