8.2 哈希表访问

本节介绍用于在哈希表中访问和存储键值对的函数。一般情况下,任意 Lisp 对象都可以作为哈希表的键,除非比较方法本身有限制;任意 Lisp 对象也都可以作为值。

Function: gethash key table &optional default

该函数在 table 中查找 key,并返回其关联的 value;如果 keytable 中不存在,则返回 default

Function: puthash key value table

该函数在 table 中为 key 建立关联,值为 value。如果 key 已存在于 table 中,则 value 会替换掉旧的值。本函数总是返回 value

Function: remhash key table

该函数从 table 中移除 key 对应的键值对(如果存在)。如果 key 不存在,remhash 不做任何操作。

Common Lisp 注意事项: 在 Common Lisp 中,若 remhash 实际移除了一个键值对,则返回非 nil 值;否则返回 nil。而在 Emacs Lisp 中,remhash 始终返回 nil

Function: clrhash table

该函数会移除哈希表 table 中的所有键值对,使其变为空表。这一操作也被称为 清空(clearing) 哈希表。clrhash 返回清空后的 table

Function: maphash function table

该函数会对 table 中的每一个键值对调用一次 function。函数 function 需接收两个参数 — table 中的一个 key(键),以及其对应的 value(值)。maphash 的返回值为 nil

允许 function 调用 puthash 为当前 key 设置新值,或调用 remhash 移除当前 key;但不允许在 function 中向 table 添加、移除或修改其他键值对。


emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© 2025 Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike