all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
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:49:19 +0100	[thread overview]
Message-ID: <trinity-663c1ba6-dd2a-4fc2-9468-af0e55f39d72-1608173359580@3c-app-mailcom-bs11> (raw)
In-Reply-To: <87zh2d1byp.fsf@fastmail.fm>



> 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"
> 
> Then type `(timfutur)` and hit RET. You'll get the return value 1005 (displayed
> also in octal and hexadecimal).
> 
> 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.

I ran the following  

ELISP> tima
(lett 845 80)
"tim_out: 1005"

ELISP> tim
*** Eval error ***  Symbol’s value as variable is void: tim

ELISP> (lett 845 90)
"tim_out: 1015"

ELISP> tim
*** Eval error ***  Symbol’s value as variable is void: tim

So this is quite different from using "setq".  The variable "tim" is
not available to all of emacs.

------- code ------

(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) ))




 
> 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
> 
>



  parent reply	other threads:[~2020-12-17  2:49 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
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 [this message]
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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=trinity-663c1ba6-dd2a-4fc2-9468-af0e55f39d72-1608173359580@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.