From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Joost Kremers Newsgroups: gmane.emacs.help Subject: Re: Understanding the "let" construct and the setting of variables Date: Thu, 17 Dec 2020 01:21:56 +0100 Message-ID: <87zh2d1byp.fsf@fastmail.fm> 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="19294"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.5.7; emacs 27.1.50 Cc: 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 02:03:27 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 1kphhV-0004w1-Ld for geh-help-gnu-emacs@m.gmane-mx.org; Thu, 17 Dec 2020 02:03:25 +0100 Original-Received: from localhost ([::1]:57940 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kphhU-0007WW-II for geh-help-gnu-emacs@m.gmane-mx.org; Wed, 16 Dec 2020 20:03:24 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41476) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kphgs-0007WO-9m for help-gnu-emacs@gnu.org; Wed, 16 Dec 2020 20:02:46 -0500 Original-Received: from out4-smtp.messagingengine.com ([66.111.4.28]:34867) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kphgq-0000JC-9A for help-gnu-emacs@gnu.org; Wed, 16 Dec 2020 20:02:46 -0500 Original-Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 0964F5C0200; Wed, 16 Dec 2020 20:02:43 -0500 (EST) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Wed, 16 Dec 2020 20:02:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.fm; h= references:from:to:cc:subject:date:in-reply-to:message-id :mime-version:content-type:content-transfer-encoding; s=fm1; bh= 0csqygVuRF4QCPchRt6o8cq6JTxHM04BMuTDwQUAL1g=; b=ffc6DHIvqtLE0tjR JgXMJc7JQvkgGBEtMNAsBNByX/tRSHIx4uYt6suuBeC8qz7BIP5TZVfsh5TpFCyn E6eNKBiUHkwriN6PNdzlxRh08IsVk9YwCYyJYUr3gJJhuF7YrO0AIn+iKHEXqnbo kbIlY8A8QnJcTmbAfutbxGjSwmXiDrEzmOp5GK1bxkjP9Y7vdogioVaOczmctfXy /fJUKyX6ZjnO0O302EubUouq7x24qQii+c3tfylrdt357/chrBCKqo5i65ADug5p ofVb3p0/S1T4ivgws74RC1QmbRaAnAvq/dOGdFy/wfYBd5i0UAaaneuOrkQNcvAL kXrvnQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=0csqygVuRF4QCPchRt6o8cq6JTxHM04BMuTDwQUAL 1g=; b=AS1LHWf+YuO5GGPGUloLZCvdcZmPsFoRJxCRVwVF8eMwX/AtN4T0IvFiT sJbbmV9RnjHZFFhIXtDCaUVDpDU5YYj4CGn4ViKC5cBOHKqt1CtXgp8gm+NCkMMc qXZ1lCZ2bGLXqrG+nm0ADh07erlAfYExPgxVdsb7OntoiKc3LwDhV0SQnDUQ5Kq9 +zzt6/WbrcnLmP1U6t5efQQ15JHEQ/zS8nRb428Q1/4YNZm3rdXnxd6lsVIjOn2K mqe1txOlrkg0zn7fGevHjcUnr8M6Vs+CmETEEWXSvcr9VDpmxdMJzoqaLS1XOdgj 8JELvnIg4R+/j2a1DtpN5NRtWruLg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudelfedgfedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpehffgfhvffuffgjkfggtgfgsehtqh ertddtreejnecuhfhrohhmpeflohhoshhtucfmrhgvmhgvrhhsuceojhhoohhsthhkrhgv mhgvrhhssehfrghsthhmrghilhdrfhhmqeenucggtffrrghtthgvrhhnpedvvdfggefhtd eihfevvddvtdejhfeljeevledtleffvefhieejkefhuddtffegfeenucfkphepleehrdel tddrvddvgedrvdegieenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpehjohhoshhtkhhrvghmvghrshesfhgrshhtmhgrihhlrdhfmh X-ME-Proxy: Original-Received: from Swift.fastmail.com (ip5f5ae0f6.dynamic.kabel-deutschland.de [95.90.224.246]) by mail.messagingengine.com (Postfix) with ESMTPA id B637024005E; Wed, 16 Dec 2020 20:02:41 -0500 (EST) In-reply-to: Received-SPF: pass client-ip=66.111.4.28; envelope-from=joostkremers@fastmail.fm; helo=out4-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-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:126435 Archived-At: On Thu, Dec 17 2020, steve-humphreys@gmx.com wrote: > I have been writing some elisp to set the time grid in the agenda. > The discussion progressed towards the use of the "let" construct. > > But, the discussion got too advanced for me to follow the different > points of view and make a decision. > > This message is for showing some examples, of how to set and use variables > in a "let", because people criticise using "setq". But discussion needs > simple examples that would not overwhelm a relative beginner. > > (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)) I'm not sure what exactly you're asking, (I'm wondering if your message is complete or was accidentally sent before you finished it), but to understan= d the problem with `setq`, evaluate your function above in the `*scratch*` buffer. (Copy the function into the `*scratch*` buffer, put the cursor right after = it and press `C-x C-e`; note that you need to add another closing parenthesis = on the last line). That will define your function and make it available to Emacs. Then open a Lisp interaction buffer with `M-x ielm RET`. You'll get a buffer called `*ielm*` with a prompt where you can type Elisp expressions that get executed right away. Type `tim` (without parentheses) and hit RET. You shou= ld get a void variable error: *** Eval error *** Symbol=E2=80=99s value as variable is void: tim" Then type `(timfutur)` and hit RET. You'll get the return value 1005 (displ= ayed also in octal and hexadecimal). Now type `tim` again at the prompt. This time, there won't be an error anym= ore. Instead you'll get the value 845. Running your function has created a global variable `tim` (plus all the oth= er variables you've setq'ed), and since there are no packages or namespaces in Elisp, your variable is now available to all of Emacs. While I was writing this, your two questions arrived: > 1. In what simple circumstances would one use a "setq" in the body of a l= et? One common idiom would be to create or consume a list inside a loop, e.g.,= =20 ``` (let ((lst (some-function-that-produces-a-list))) (while (some-condition-on (car lst)) (do-something-with (car lst)) (setq lst (cdr lst)))) ``` Nowadays such an idiom would more often be handled with a higher-order func= tion of the map/reduce/filter-family, but there may be situations in which that doesn't work. Another example would be the case where you want to modify a value based on= some set of conditions, e.g.,: ``` (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)) ``` You could probably rewrite this without `setq` using `let*` and some intermediate variables, but sometimes I find using `setq` to be clearer, especially if you have multiple values that are interdependent in complex w= ays. > 2. What simple option does one have that is more advantageous than using = a "setq"? `let*` in the example function you gave above. For creating or consuming a = list, there's the map/filter/reduce-family or cl-loop. Not sure if that makes it any easier for a relative beginner :-/ but I hope= it helps a bit anyway. --=20 Joost Kremers Life has its moments