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

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

Re: Printing alist pairs to a dedicated buffur


From: Emanuel Berg
Subject: Re: Printing alist pairs to a dedicated buffur
Date: Sat, 20 Apr 2024 12:08:39 +0200
User-agent: Gnus/5.13 (Gnus v5.13)

Manuel Giraud via Users list for the GNU Emacs text editor wrote:

>> This is how I am filling my alist named tema-lugar
>>
>>
>> (defun tema-mark (kfrz)
>>   "Associate line number at cursor position with key phrase KFRZ."
>>   (interactive "sString: ")
>>
>>   (let ( (lnum (line-number-at-pos)) )
>>
>>     (setq-local tema-lugar
>>                  (append tema-lugar
>>                           (list (cons kfrz lnum)))) ))
>
> Maybe your error comes from this `setq-local'. I have used
> `defvar' which will create a global (i.e. visible everywhere
> in Emacs) variable. `setq-local' or `defvar-local' are used
> for *buffer-local* variables. Buffer-local variables are
> visible only from the buffer they were created.

Don't use `defvar' if it can be avoided as that creates global
dynamic/special variables, `setq' creates global
static/lexical variables which isn't much better - unless
there is a variable by that name present, then that is used
instead. That kind of `setq' use is not wrong, actually it
is good.

But yes, variables in Emacs are complicated and one problem is
that so much is dependent on the situation while the commands
look the same.

Anyway here is an interesting case, a combination of a for and
while loop, and we see the kind of good `setq' use I mean,
which do not create a global variable but sets an existing
one, "lst", instead.

(defun count-list (lst)
  (cl-loop
     with res = ()
     for l in lst while lst do
     (push (list (cl-count l lst) l) res)
     (setq lst (cl-remove l lst))
     finally return (cl-sort res #'>= :key #'car) ))

;; (count-list '(a b c d e a b c d a b c a b a))
;; -> ((5 a) (4 b) (3 c) (2 d) (1 e))

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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