0

I have a number of "alists".

(defconst assoc-table-1 
  '( ("frantic" . "fN") ("authentic" . "atN") ))

(defconst assoc-table-2
  '( ("graphology" . "Gol") ("phrenology" . "fnol") ))

I then construct a "list of alists".

(defun assoc-list ()
  "TODO"

  (let* ( (name "assoc-table-")
      (counter 1)
      (table '()) )

    (while (boundp (intern (concat name (number-to-string counter))))

      (setq table
         (append table
            (symbol-value (intern (concat name (number-to-string counter))))))
  (message "%s" (concat name (number-to-string counter)))
  (setq counter (1+ counter)))

table))

which I pass to this function

(defun shorten ()
  "Shorten word at point according to specific rules."
  (interactive)

  (let* ( (bounds  (bounds-of-thing-at-point 'word))
      (word (downcase (buffer-substring (car bounds) (cdr bounds))))
      (rplc "") )

    (goto-char (car bounds))
    (add-to-list 'nscrip word)

    (dolist (aggr (assoc-list))
       (setq rplc (cdr (assoc word aggr)))
       (unless (null rplc)
          (add-to-list 'nscrip
             (replace-regexp-in-string word rplc word))))

Thu function above fails on the first element because assoc-list consists of a "list of alists", but (assoc word aggr) expects a list in aggr, but is getting an element with a dot in it.

How can I solve the problem encountered by (assoc word aggr).

5
  • I think you forgot to include the evaluated BODY; your dolist parentheses are not even closed. A minimal example would probably help.
    – Ajned
    Commented Aug 26, 2022 at 6:32
  • I added the (assoc word aggr) part that generates the problem.
    – Dilna
    Commented Aug 26, 2022 at 6:49
  • 2
    There's no question here, just a piece of code and an unclear discussion of how it fails. You don't explain what you're trying to do and you don't explain what the problem is in a comprehensible way. Commented Aug 26, 2022 at 7:54
  • @Hovlar What is word? If it's not defined as a variable you should use (setq rplc (cdr (assoc 'word aggr))) in your second line. Note the apostrophe before word. As Gilles says you should provide a complete example with all your variables and lists declared
    – Ajned
    Commented Aug 26, 2022 at 8:01
  • Have provided a complete example.
    – Dilna
    Commented Aug 26, 2022 at 8:19

1 Answer 1

2

The problem is that assoc-list does not produce a list of alists, it merges the assoc-list-* alists into one big alist. This is because you use append to mash the alists together.

Here is a version that does what you intend:

(defun assoc-list ()
  "TODO"

  (let* ( (name "assoc-table-")
      (counter 1)
      table)

    (while (boundp (intern (concat name (number-to-string counter))))

      (push
       (symbol-value (intern (concat name (number-to-string counter))))
       table)
  (message "%s" (concat name (number-to-string counter)))
  (setq counter (1+ counter)))

table))

In detail: set table to nil (the empty list). Then push each constituent alist into table.

You should look at the docstrings of both push and append to appreciate the difference.

General remark: evaluating your code in the scratch buffer makes it easy to see what kind of object yr code is returning. Experimentation of this kind is what makes lisp so much fun and you should be doing way more of that instead of asking here every time.

2
  • Right. I can see now how it is producing a list of alists. I do get ((("graphology" . "Gol") ("phrenology" . "fnol")) (("frantic" . "fN") ("authentic" . "atN"))).
    – Dilna
    Commented Aug 26, 2022 at 9:13
  • +1 particularly for the closing remark, which I hope the OP will heed.
    – NickD
    Commented Aug 26, 2022 at 14:53

Your Answer

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

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