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

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

Re: Looping lists through mapcar


From: Jean Louis
Subject: Re: Looping lists through mapcar
Date: Wed, 24 Aug 2022 19:17:11 +0300
User-agent: Mutt/+ () (2022-06-11)

To test my function, you first evaluate these:

(defconst andromeda-assoc-table-1
  '(("OrycteropusAfer" . "Aardvark")
    ("VicugnaPacos" . "Alpaca")
    ("MyrmecophagaTridactyla" . "Anteater")))

;; So I evaluate it, and it is there:
;; andromeda-assoc-table-1 ⇒ (("OrycteropusAfer" . "Aardvark") ("VicugnaPacos" 
. "Alpaca") ("MyrmecophagaTridactyla" . "Anteater"))

(defconst andromeda-assoc-table-2
  '(("Dasypodidae" . "Armadillo")
    ("TaxideaTaxus" . "Badger")
    ("Beaver" . "Beaver")))

;; andromeda-assoc-table-2 ⇒ (("Dasypodidae" . "Armadillo") ("TaxideaTaxus" . 
"Badger") ("Beaver" . "Beaver"))

(defconst andromeda-assoc-table-3
'(("LynxRufus" . "Bobcat")
  ("LepomisMacrochirus" . "Bluegill")
  ("RangiferTarandus" . "Caribou")))

;; andromeda-assoc-table-3 ⇒ (("LynxRufus" . "Bobcat") ("LepomisMacrochirus" . 
"Bluegill") ("RangiferTarandus" . "Caribou"))


(defun my-assoc-lists ()
  (let* ((name "andromeda-assoc-table-")
         (list '())
         (counter 1))
    (while (boundp (intern (concat name (number-to-string counter))))
      (setq list (append list (symbol-value (intern (concat name 
(number-to-string counter))))))
      (setq counter (1+ counter)))
    list))

Then you evaluate this function:

;; (my-assoc-lists) ⇒ (("OrycteropusAfer" . "Aardvark") ("VicugnaPacos" . 
"Alpaca") ("MyrmecophagaTridactyla" . "Anteater") ("Dasypodidae" . "Armadillo") 
("TaxideaTaxus" . "Badger") ("Beaver" . "Beaver") ("LynxRufus" . "Bobcat") 
("LepomisMacrochirus" . "Bluegill") ("RangiferTarandus" . "Caribou"))

That way you can add any number instead of XX
 `andromeda-assoc-table-XX' and function will add them all
 together. You need not update your other function with new
 symbols such as `andromeda-assoc-table-23' or number of them.

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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