help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Making ielm behave like a shell (getting to previous commands using


From: Drew Adams
Subject: RE: Making ielm behave like a shell (getting to previous commands using the up-arrow key)
Date: Fri, 18 Dec 2020 09:14:10 -0800 (PST)

> First step:
> 
> (let* ((mailing-hash (rcd-db-table-id-hash "mailinglistoptions" option *cf*))
>        (mid (gethash 'mailinglistoptions_accounts mailing-hash))
>        (mid-hash (rcd-db-table-id-hash "accounts" mid *cf*))
>        (accounts-hash (rcd-db-table-id-hash "accounts" mid *cf*))
>        (email-list (rcd-email-list mid))))
> 
> Second step:
> 
> (let* ((setq mailing-hash (rcd-db-table-id-hash "mailinglistoptions" option 
> *cf*))
>        (mid (gethash 'mailinglistoptions_accounts mailing-hash))
>        (mid-hash (rcd-db-table-id-hash "accounts" mid *cf*))
>        (accounts-hash (rcd-db-table-id-hash "accounts" mid *cf*))
>        (email-list (rcd-email-list mid))))

Nope.  That raises an error telling you
 "`let' bindings can have only one value-form".

FYI, that `let' has an empty body - it has only bindings.
That's not an error, but it might let you know that
something might not be as you hope.

More importantly, this "binding" is erroneous:

(setq mailing-hash (rcd-db-table-id-hash ...))

A `let' binding is either just a symbol (implicit binding
to `nil') or a two-element list of a symbol and a sexp.

That "binding" looks at first like it's binding symbol
`setq' to the value of variable `mailing-hash'.  But
the list doesn't end there - it's not a 2-element list
(which is what the error message tells you).

If you intend to use `setq' to assign a value to a
variable, instead of let-binding it, then move the
`setq' to the body of the `let'.

CAN you use a `setq' or other procedural code inside
a let-binding?  Sure, if you want to.  Just provide a
variable to bind to it.

(let* ((fred      42)
       (_IGNORED  (setq fred  'nope-24))
       (_NADA     (message "FRED: %S" fred)))
  ;; Empty body, if you like
  )

(Nothing special about the variable names.  But an
underscore prefix is sometimes used conventionally
to tell human readers that the thingy isn't really
used or isn't used in some particular way.  Here,
I use it to tell myself that the variable is used
only to be able to evaluate some sexp for its side
effect.  And I use uppercase just to make this
weirdness more obvious to myself.)

You can also bind the same variable multiple times,
if you like:

(let* ((fred   42)
       (alice  (foo fred))
       (fred   (if (> fred 0) 24 'derf))
       (fred   '(nah dont-do-that at-all)))
 (message "FRED: %S" fred))

IOW:

You can put any kind of procedural code in a `let' body
or a `let' binding.  If in a binding, it needs to be as
a sexp to evaluate and bind to some variable.

You can use multiple, sequential bindings for the same
variable, if you use `let*'.

You can also use the same variable name multiple times
with plain `let', but that's typically useless and not
what you intend.

(let ((fred  42) ; Not used anywhere
      (fred  '(nah dont-do-that at-all)))
 (message "FRED: %S" fred))

> Third step:
> 
> (let* ((mailing-hash (rcd-db-table-id-hash "mailinglistoptions" option *cf*))
>        (setq mid (gethash 'mailinglistoptions_accounts mailing-hash))
         ^^^^^^^^^^^^^^^^^^^...
Same problem as above.

> How would you do message calls there?

See above.  Put any procedural code you want to use
in the bindings part into a binding: provide a var
that you use or don't use, binding it to the code
you want to evaluate for its side effects.

Is this kind of thing great style?  Maybe not.  But
you can do it.

Being able to add `message' calls wherever you want
can be helpful.

Whether to use multiple `let*' bindings for the same
variable, versus the alternative of using multiple
`let(*)' forms, is a question of style and how you
want to work.  Coding is not only about the final
result; it can also be about intermediate forms that
are easy to work with, that talk to you, or are
otherwise convenient (for you) in some way.

Another thing you can add, as an alternative to
`message' calls, are calls to `debug'.  Those are
essentially breakpoints.  They let you know which
branch of code gets executed, opening the debugger
when encountered.

(let* ((fred   42)
       (_ZIP   (setq fred  'nope-24))
       (_NADA  (when (toto) (debug nil fred))))
  (unless (titi) (debug))
  ;;...
  )

`C-h f debug'...



reply via email to

[Prev in Thread] Current Thread [Next in Thread]