gpt4 book ai didi

emacs - gethash 不适用于字符串键

转载 作者:行者123 更新时间:2023-12-01 23:01:51 24 4
gpt4 key购买 nike

学习elisp中的hash表,我试着写了一个简单的例子:

(setq animals (make-hash-table))
(puthash "tiger" 120 animals)
(gethash "tiger" animals)

当我一行一行地执行它们时,调用 gethash返回 nil ,尽管事实上,当我评估动物符号时,emacs 会打印:
#s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 
0.8 data ("tiger" 120 ...))

所以,“老虎”就在那里,但由于某种原因,gethash 没有返回它。

怎么了?

docs for hash table functions

最佳答案

哈希表的默认测试是 eql .每次键入字符串时,都会创建一个不同的字符串,因此它们不是 eql对彼此。

(eql "tiger" "tiger") => nil

您需要使用 equal作为测试:
(setq animals (make-hash-table :test 'equal))

或者使用符号而不是字符串作为表中的键;由于符号是固定的,因此两次输入相同的符号名称会导致 eql对象。

关于emacs - gethash 不适用于字符串键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21526052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com