Assoc ใน alist ที่ซ้อนกัน

Nov 19 2020

ฉันมีสิ่งต่อไปนี้ซ้อนกัน:

(setq x '(foo . ((bar . ((chocolate . "edible") (gold . "inedible")))
                 (jar . "glass"))))

จะเข้าได้(chocolate . "edible")อย่างไร?

ฉันอ่านคำถามนี้และและสิ่งนี้

แต่แตกต่างจาก q1 ฉันไม่ทราบ "เส้นทาง" ไปยังค่าและไม่เหมือนกับ q2 ฉันต้องการใช้ Elisp นอกจากนี้ฉันยังมีรายการที่ใหญ่กว่าซึ่งสามารถมี "ความลึก" ได้ 2 - 5 (โดยความลึกฉันหมายถึงเพื่อนร่วมทีม)

จนถึงตอนนี้นี่คือสิ่งที่ฉันสามารถปรุงได้:

(defun assoc-recur (key list)
  (if (listp (cdr list))
      (assoc key (cdr list))
    (assoc-reccur key (cdr list))))

เห็นได้ชัดว่ารหัสนี้ใช้งานได้ตราบเท่าที่ค่าไม่ใช่รายการของการเปลี่ยนแปลงเช่น (bar . ((..))

ฉันจะเข้าถึงค่าใน alist ที่ซ้อนกับ vanilla Elisp (โดยไม่ใช้การจำลอง CL) ได้อย่างไร หรือฉันควรจะยอมแพ้และติดตั้ง CL api แล้วลอง q2?

ไวยากรณ์ที่ฉันกำลังมองหาเป็นสิ่งที่ต้องการ (func key list)

ps:ฉันค่อนข้างใหม่สำหรับ Emacs ดังนั้นฉันอาจจะพลาดฟังก์ชั่นที่สะดวกสบาย

คำตอบ

3 Basil Nov 19 2020 at 22:55

ฉันมีสิ่งต่อไปนี้ซ้อนกัน:

ตัวอย่างไม่แสดงความเป็นจริงเพราะองค์ประกอบแรกfooไม่ใช่เซลล์ข้อเสีย โดยส่วนตัวฉันเรียกมันว่าต้นไม้ ฟังก์ชั่นเช่นassoc-stringนี้อาจจัดการกับสิ่งนี้คนอื่น ๆ อาจละเลยองค์ประกอบดังกล่าว แต่โดยทั่วไปแล้วฟังก์ชั่น alist คาดว่าทุกองค์ประกอบจะเป็นข้อเสียของรถยนต์และ cdr ดู(info "(elisp) Lists")และโหนดย่อย

จนถึงตอนนี้นี่คือสิ่งที่ฉันสามารถปรุงได้:

Elisp ไม่จัดการการเรียกซ้ำอย่างมีประสิทธิภาพดังนั้นฉันขอแนะนำให้หลีกเลี่ยงโดยทั่วไปถ้าเป็นไปได้ มิฉะนั้นคุณอาจถึงmax-specpdl-sizeขีด จำกัด

ไม่เหมือนกับ q1 ฉันไม่ทราบ "เส้นทาง" ไปยังค่า

นอกจากนี้ฉันยังมีรายการที่ใหญ่กว่าซึ่งสามารถมี "ความลึก" ได้ 2 - 5 (โดยความลึกฉันหมายถึงเพื่อนร่วมทีม)

เนื่องจากความผิดปกติของโครงสร้างข้อมูลนี้ฉันขอแนะนำให้แบนรายการก่อนที่จะค้นหาสิ่งต่างๆในนั้น สิ่งนี้จะช่วยลดความซับซ้อนของโค้ดได้อย่างมากโดยต้องเสียเวลาและพื้นที่ ใน Emacs 27:

(setq x '(foo
          (bar (chocolate . "edible")
               (gold . "inedible"))
          (jar . "glass")))
(cadr (memq 'chocolate (flatten-tree x))) ; => "edible"

นี่คือการใช้งานในปัจจุบันflatten-treeในกรณีที่คุณใช้ Emacs เวอร์ชันเก่ากว่า:

(defun flatten-tree (tree)
  "Return a \"flattened\" copy of TREE.
In other words, return a list of the non-nil terminal nodes, or
leaves, of the tree of cons cells rooted at TREE.  Leaves in the
returned list are in the same order as in TREE.

\(flatten-tree \\='(1 (2 . 3) nil (4 5 (6)) 7))
=> (1 2 3 4 5 6 7)"
  (let (elems)
    (while (consp tree)
      (let ((elem (pop tree)))
        (while (consp elem)
          (push (cdr elem) tree)
          (setq elem (car elem)))
        (if elem (push elem elems))))
    (if tree (push tree elems))
    (nreverse elems)))

หรืออีกวิธีหนึ่งคือคุณสามารถทำการค้นหาแบบต้นไม้แรกแบบเชิงลึกซ้ำ ๆ เป็นการแลกเปลี่ยนปัญหาการเรียกซ้ำของ Elisp สำหรับรหัสที่ซับซ้อนมากขึ้น นี่คือตัวอย่างของ DFS บน HTML DOM ที่นำมาจากhttps://github.com/abo-abo/swiper/pull/1593#issuecomment-392587760 :

(defun counsel--firefox-bookmarks-libxml ()
  "Parse current buffer contents as Firefox HTML bookmarks.
Return list of propertized string candidates for
`counsel-firefox-bookmarks'.
Note: This function requires libxml2 support."
  ;; Perform iterative pre-order depth-first search instead of using
  ;; `dom.el' because the latter is new to Emacs 25 and uses recursion.
  (let ((stack (cddr (libxml-parse-html-region (point-min) (point-max))))
        cands)
    (while (let ((node (pop stack)))
             (if (eq (car-safe node) 'a)
                 (let* ((text (cl-caddr node))
                        (attrs (cadr node))
                        (href (cdr (assq 'href attrs)))
                        (tags (cdr (assq 'tags attrs))))
                   (unless (zerop (length href))
                     (push (counsel--firefox-bookmarks-cand href text tags)
                           cands)))
               (dolist (child (nreverse (cddr node)))
                 (when (consp child)
                   (push child stack))))
             stack))
    cands))

ในกรณีของคุณการwhileวนซ้ำจะสิ้นสุดลงตามตำแหน่งของคีย์ที่ต้องการ

หรือขอแนะนำให้จัดโครงสร้างข้อมูลของคุณให้เป็นปกติมากขึ้น ;)

xuchunyang Nov 20 2020 at 00:30

คุณสามารถใช้มาโครในตัวlet-alistเพื่อเข้าถึงค่าจาก alist ที่ซ้อนกันเช่น

(let-alist
    '((foo . ((bar . ((chocolate . "edible") (gold . "inedible")))
              (jar . "glass"))))
  .foo.bar.chocolate)
;; => "edible"

และของคุณxไม่ใช่ alist alist คือรายการของคู่คีย์ - ค่าเช่น, ((key1 . val1) (key2 . val2) ...).