0

I have a collection of alists, ptc-1 through ptc-5. Would it be possible to make the following code more compact, rather having to add more sections if there are more alists?

(let ( (v "") )

  (setq v (cdr (assoc word ptc-1)))
  (unless (null v)
     (add-to-list 'ptc
         (replace-regexp-in-string word v word)))

  (setq v (cdr (assoc word alist-2)))
  (unless (null v)
     (add-to-list 'ptc
         (replace-regexp-in-string word v word)))
  
  (setq v (cdr (assoc word alist-3)))
  (unless (null v)
     (add-to-list 'ptc
         (replace-regexp-in-string word v word)))

  (setq v (cdr (assoc word alist-4)))
  (unless (null v)
     (add-to-list 'ptc
         (replace-regexp-in-string word v word)))

  (setq v (cdr (assoc word alist-5)))
  (unless (null v)
     (add-to-list 'ptc
         (replace-regexp-in-string word v word))) )

1 Answer 1

0

You should be able to loop over each of the alists with a dolist loop macro. So something like:

(let ((v ""))
  (dolist (alist (list ptc-1 ptc-2 ptc-3 ptc-4 ptc-5))
    (setq v (cdr (assoc word alist)))
    (unless (null v)
       (add-to-list 'ptc
           (replace-regexp-in-string word v word)))
  v)

For a shorter example, (dolist (x '(64 65 66)) (insert x)) would insert ABC into the current buffer.

I also took the liberty of assuming the list of alists contained ptc-1 through ptc-5, unlike your example above which used alist-2 through alist-5.

However, just as a suggestion, if you are iterating over multiple alists like this, it might help to think of one data structure that you could operate on instead of 5 different parallel structures. I don't have a sense of the problem space here so I can't make any suggestions, but might be something worth considering.

5
  • Because of the possibility of having many lists, I would prefer to construct the alist name by having ptc-N, then calling each one, without having to make a big collection such as your alist.
    – Dilna
    Aug 22, 2022 at 3:09
  • In the example above alist is a variable name representing each of the alists you mentioned. We could also format this as (defvar alists-to-iterate (list ptc-1 ptc-2 ...)) (dolist (alist alists-to-iterate) ...). So if you have more lists to iterate you would add them to alists-to-iterate. Otherwise, perhaps I'm not understanding your intention here. I thought you wanted to avoid adding additional blocks for each alist that you needed to iterate. Could you clarify your intent a little?
    – dgtized
    Aug 22, 2022 at 3:16
  • Just a variable then. Thought that alist (list ... would make one massive alist with all the contents of the lists in it.
    – Dilna
    Aug 22, 2022 at 3:23
  • Suppose the list names are painfully long. such as andromeda-dictionary-word-assoc-list-N etc, and I have 90 of them (N=90). Can I avoid having to list them one by one?
    – Dilna
    Aug 22, 2022 at 3:27
  • Was thinking to construct the alist symbols with mapcar.
    – Dilna
    Aug 22, 2022 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.