From: steve-humphreys@gmx.com
To: Joost Kremers <joostkremers@fastmail.fm>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Understanding the "let" construct and the setting of variables
Date: Thu, 17 Dec 2020 03:08:43 +0100 [thread overview]
Message-ID: <trinity-aedd5e45-3f76-4861-a7df-4040c6002b9f-1608170923555@3c-app-mailcom-bs11> (raw)
In-Reply-To: <87zh2d1byp.fsf@fastmail.fm>
Finally I got some useful information (ielm). Fantastic.
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.
QUESTION: What happens if one does a "setq" on a variable defined in the binding part of the
"let"? Can one do that?
QUESTION: What goes in the body of a "let"? I would think I can use the variables defined within
the binding section of "let".
QUESTION: Can one update a variable in the body of the "let" (the variable being defined within
the binding section of "let").
> Sent: Thursday, December 17, 2020 at 1:21 AM
> From: "Joost Kremers" <joostkremers@fastmail.fm>
> To: steve-humphreys@gmx.com
> Cc: help-gnu-emacs@gnu.org
> Subject: Re: Understanding the "let" construct and the setting of variables
>
>
> 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 understand 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 should
> get a void variable error:
>
> *** Eval error *** Symbol’s value as variable is void: tim"
Correct. That's what I got.
> Then type `(timfutur)` and hit RET. You'll get the return value 1005 (displayed
> also in octal and hexadecimal).
Ok, got that.
--------
ELISP> (timfutur)
1005 (#o1755, #x3ed)
> Now type `tim` again at the prompt. This time, there won't be an error anymore.
> Instead you'll get the value 845.
> Running your 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.
ELISP> tim
845 (#o1515, #x34d)
> While I was writing this, your two questions arrived:
>
> > 1. In what simple circumstances would one use a "setq" in the body of a let?
>
> One common idiom would be to create or consume a list inside a loop, e.g.,
>
> ```
> (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 function
> 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 ways.
>
> > 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.
>
>
> --
> Joost Kremers
> Life has its moments
>
>
next prev parent reply other threads:[~2020-12-17 2:08 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 [this message]
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
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-aedd5e45-3f76-4861-a7df-4040c6002b9f-1608170923555@3c-app-mailcom-bs11 \
--to=steve-humphreys@gmx.com \
--cc=help-gnu-emacs@gnu.org \
--cc=joostkremers@fastmail.fm \
/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).