Pergunta

Estou tentando criar um tipo de hash "dicionário" - ou seja, com uma string como chave. Isso é possível ou sábio em Lisp?

Percebi que isso funciona conforme o esperado:

> (setq table (make-hash-table))
#<HASH-TABLE :TEST EQL size 0/60 #x91AFA46>
> (setf (gethash 1 table) "one")
"one"
> (gethash 1 table)
"one"

No entanto, o seguinte não é:

> (setq table (make-hash-table))
#<HASH-TABLE :TEST EQL size 0/60 #x91AFA0E>
> table
#<HASH-TABLE :TEST EQL size 0/60 #x91AFA0E>
> (setf (gethash "one" table) 1)
1
> (gethash "one" table)
NIL
NIL
Foi útil?

Solução

Você precisa fazer a tabela de hash que usa 'igual se' eql. 'O EQL não avalia duas cordas com o mesmo conteúdo para' t, enquanto 'igual o faz.

Aqui está como você faz isso:

(make-hash-table :test 'equal)

Como o Skypher observou, você também pode usar 'Equalp, se desejar um hash de string que insensível ao caso.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top