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 04:12:07 +0100 Message-ID: References: <87zh2d1byp.fsf@fastmail.fm> 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="6150"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Joost Kremers , help-gnu-emacs@gnu.org 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 04:13:11 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 1kpjj4-0001Vt-Dy for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 17 Dec 2020 04:13:10 +0100 Original-Received: from localhost ([::1]:37870 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kpjj3-00031c-EY for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 16 Dec 2020 22:13:09 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42016) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpjiD-000307-Cs for help-gnu-emacs@gnu.org; Wed, 16 Dec 2020 22:12:17 -0500 Original-Received: from mout.gmx.net ([212.227.17.20]:48085) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kpji9-0007aj-Gz for help-gnu-emacs@gnu.org; Wed, 16 Dec 2020 22:12:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1608174727; bh=9XBJSm4rSNxTkxwQlmwDCAa0TBOkogJTKopq7hUN4q4=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=GYJ/z+tamMSTAjPU6EpXarIpIrTMeLVQ9Pmz3DuJlPlGnXHFmZsN1AxWVCrpZg4/p C9SBo7RF+nxKQJzmDYwoLNDVLDfrKlBqBoF6MSl0M8yaaE8e+ZW5/o1twi0OqztDMX G4x/N7gTqNjnm61HxQ4PHRHVvAVTLirfTpQzRVbk= 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 04:12:07 +0100 Importance: normal Sensitivity: Normal In-Reply-To: X-UI-Message-Type: mail X-Priority: 3 X-Provags-ID: V03:K1:bonTT1Pz500nOMYfElm8FHs4vz+zDz62WUq0f7rBUiNxxGjYbFrSafzmEnaduSvI3vEKv QKjqOVXTG43bfvHLyrptp3vA5i0PUZw4EQJnQ+zPeTSt6rIeH4VM0U0bLSFOb+CzHLOXruAzBGNz dU5wd6MJ9Q49ifd3/oJZgJ0n3lwRto0sJdejqf23k/M6K3D9YbvT7U4J+SuJGr2I1QIsvp81uhaq vDW0wQdzPzAbxb5IUSGWO2BPuHxZjFSp2k1TZgLXpEFwjHYmqLiJAPnQ+8HcgGdc0VLjHN9uacXf 6E= X-UI-Out-Filterresults: notjunk:1;V03:K0:sUJ2cuZ/0I4=:iMfBcZ0gszISLLAZb5hiTa F/Qk9ioVkL+RYfVUNQ2mm/rUtGrHOR1t5g+iSJwdaZT11l2khLT8lV8iX23LoI0TPD2uzrsN7 aKCd5G4WWnyVCRWS/p5y4j2iaU4jp7l2I28Wzt91kvzMZwhDzPXelhlauejpY4eAA78VSmlBb SdaV/BjyZ5hrSVKjmTl4+RbzLjewqrKUtaCQy3JoaJmI311s7Mj0b2T5U2WXxb4DrVYjuuYs/ AL5dBnWV0euOAxQ77vJUw+mNsZxikRX6KKLv49Hy352Pr5izEMNg6pa0Mp0p5NqRyXIcbqCrX VmgcyUUJyZ2Uh8GLkNoEC6/8NI4ylrcp8OqIarCzUbVC0ohD0znnsJ6I5tfhL1IO3/amYW0J4 NAT8GjLPCI3JfhmJMpovJRjNhlFJ4fZqBWGjHAxNbAImr56sVNt/kcyzs3H8RWQuUGzT7zCsf gcDd+DXBDnJQVEfZ2ZGNLogsbh9Ikzwq4IV79iXsa+TJvqmpEuHkMhBBImIhN991EJWhzK0aF wjcd/WNyDvdCAi5MCY3p34QI4jM14ptY1p6gX67grbM6aUKnTjm/YgHEtFgYzeBmJT7ccO1TR 1DqKiaJrMzJog= Received-SPF: pass client-ip=212.227.17.20; 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:126442 Archived-At: I want the function "lett" to return "tim_out"=2E I understand that the la= st command=20 is the output of the function=2E But should this be done in the binding p= art or in the body part of the "let"=2E Or outside the "let"=2E =20 (defun lett (tim tsk) (let* ( (thr (/ tim 100)) (tmn (- tim (* thr 100))) (tinc_mn (+ tmn tsk)) (tinc_hr (/ (+ tmn tsk) 60)) (tinc_mn (- tinc_mn (* tinc_hr 60))) (thr_futur (* (+ thr tinc_hr) 100)) (tmn_futur tinc_mn) (tim_out (+ thr_futur tmn_futur)) ) ;; --- body of let ---- (message "tim_out: %d" tim_out) )) > Sent: Thursday, December 17, 2020 at 3:08 AM > From: steve-humphreys@gmx=2Ecom > To: "Joost Kremers" > Cc: help-gnu-emacs@gnu=2Eorg > Subject: Re: Understanding the "let" construct and the setting of variab= les > > Finally I got some useful information (ielm)=2E Fantastic=2E=20 >=20 > ACCEPTED: Running function has created a global variable `tim` (plus all= the other > variables you've setq'ed), and since there are no packages or namespaces= in > Elisp, your variable is now available to all of Emacs=2E >=20 > QUESTION: What happens if one does a "setq" on a variable defined in the= binding part of the > "let"? Can one do that? >=20 > QUESTION: What goes in the body of a "let"? I would think I can use the= variables defined within > the binding section of "let"=2E >=20 > QUESTION: Can one update a variable in the body of the "let" (the variab= le being defined within > the binding section of "let")=2E >=20 >=20 >=20 >=20 > > Sent: Thursday, December 17, 2020 at 1:21 AM > > From: "Joost Kremers" > > To: steve-humphreys@gmx=2Ecom > > Cc: help-gnu-emacs@gnu=2Eorg > > Subject: Re: Understanding the "let" construct and the setting of vari= ables > > > >=20 > > On Thu, Dec 17 2020, steve-humphreys@gmx=2Ecom wrote: > > > I have been writing some elisp to set the time grid in the agenda=2E > > > The discussion progressed towards the use of the "let" construct=2E > > > > > > But, the discussion got too advanced for me to follow the different > > > points of view and make a decision=2E > > > > > > This message is for showing some examples, of how to set and use var= iables > > > in a "let", because people criticise using "setq"=2E But discussion= needs > > > simple examples that would not overwhelm a relative beginner=2E > > > > > > (defun timfutur () > > > (interactive) > > > (setq tim 845) > > > (setq tsk 80) > > > > > > (setq thr (/ tim 100)) > > > (setq tmn (- tim (* thr 100))) > > > > > > (setq tinc_mn (+ tmn tsk)) > > > (setq tinc_hr (/ (+ tmn tsk) 60)) > > > (setq tinc_mn (- tinc_mn (* tinc_hr 60)) ) > > > > > > (setq thr_futur (* (+ thr tinc_hr) 100) ) > > > (setq tmn_futur tinc_mn) > > > (setq tim_out (+ thr_futur tmn_futur)) > >=20 > > I'm not sure what exactly you're asking, (I'm wondering if your messag= e is > > complete or was accidentally sent before you finished it), but to unde= rstand the > > problem with `setq`, evaluate your function above in the `*scratch*` b= uffer=2E > > (Copy the function into the `*scratch*` buffer, put the cursor right a= fter it > > and press `C-x C-e`; note that you need to add another closing parenth= esis on > > the last line)=2E That will define your function and make it available= to > > Emacs=2E > >=20 > > Then open a Lisp interaction buffer with `M-x ielm RET`=2E You'll get = a buffer > > called `*ielm*` with a prompt where you can type Elisp expressions tha= t get > > executed right away=2E Type `tim` (without parentheses) and hit RET=2E= You should > > get a void variable error: > >=20 > > *** Eval error *** Symbol=E2=80=99s value as variable is void: ti= m" >=20 > Correct=2E That's what I got=2E > =20 > > Then type `(timfutur)` and hit RET=2E You'll get the return value 1005= (displayed > > also in octal and hexadecimal)=2E >=20 > Ok, got that=2E =20 > -------- > ELISP> (timfutur) > 1005 (#o1755, #x3ed) >=20 > > Now type `tim` again at the prompt=2E This time, there won't be an err= or anymore=2E > > Instead you'll get the value 845=2E > =20 > > Running your function has created a global variable `tim` (plus all th= e other > > variables you've setq'ed), and since there are no packages or namespac= es in > > Elisp, your variable is now available to all of Emacs=2E >=20 > ELISP> tim > 845 (#o1515, #x34d) > =20 > > While I was writing this, your two questions arrived: > >=20 > > > 1=2E In what simple circumstances would one use a "setq" in the body= of a let? > >=20 > > One common idiom would be to create or consume a list inside a loop, e= =2Eg=2E,=20 > >=20 > > ``` > > (let ((lst (some-function-that-produces-a-list))) > > (while (some-condition-on (car lst)) > > (do-something-with (car lst)) > > (setq lst (cdr lst)))) > > ``` > >=20 > > Nowadays such an idiom would more often be handled with a higher-order= function > > of the map/reduce/filter-family, but there may be situations in which = that > > doesn't work=2E > >=20 > > Another example would be the case where you want to modify a value bas= ed on some > > set of conditions, e=2Eg=2E,: > >=20 > > ``` > > (let ((x (get-some-value)) > > (y (get-some-other-value))) > > (cond > > ((some-condition-on y) > > (setq x (do-something-with y))) > > ((some-other-condition-on y) > > (setq x (do-something-else-with x))) > > (:otherwise > > (setq y nil))) > > (now-do-something-with x y)) > > ``` > >=20 > > You could probably rewrite this without `setq` using `let*` and some > > intermediate variables, but sometimes I find using `setq` to be cleare= r, > > especially if you have multiple values that are interdependent in comp= lex ways=2E > >=20 > > > 2=2E What simple option does one have that is more advantageous than= using a "setq"? > >=20 > > `let*` in the example function you gave above=2E For creating or consu= ming a list, > > there's the map/filter/reduce-family or cl-loop=2E > >=20 > > Not sure if that makes it any easier for a relative beginner :-/ but I= hope it > > helps a bit anyway=2E > >=20 > >=20 > > --=20 > > Joost Kremers > > Life has its moments > >=20 > > >=20 >