4

As a result of this investigation, we know that using special functions (optimized for certain types of data) is a good idea. I would like to create a hash table, but this source says:

Syntax:

make-hash-table &key test size rehash-size rehash-threshold => hash-table

Arguments and Values:

test - a designator for one of the functions eq, eql, equal, or equalp. The default is eql.

Well, well. What if I wanna speed up my program when I work with strings and use string-equal for instance? There is no restrictions regarding equality predicates for other high-order functions, but for make-hash-table they are. What is the reason of that?

Bonus "history-of-lisp" question: why the function gethash is named so? Why not get-hash?

Community
  • 1
  • 1
Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
  • 1
    Bonus-answer: In [naming convensions](http://www.cliki.net/Naming+conventions) it hints that it migth depend on what comes after `get`. If it's a hyphenated value you hyphenate like in `get-setf-method` but not if there is only one word like `gethash`. CL is a join of several already existing LISPs so it's not strange the naming convention looks a little bumpy and some of CL looks like it's designed by different designers :-p – Sylwester Jun 10 '14 at 16:41
  • That's actually consistent with the rule for appending `p` to predicates! – Stefan Schmiedl Jun 11 '14 at 18:37

1 Answers1

5

What is a Hash Table

A hash table works by computing a hash of an object and storing the object in a, well, table, indexed by the hash. The test function must satisfy the fundamental condition that for all objects x and y:

(FUNCALL test-function x y) ⇒
(= (FUNCALL hash-function x) (FUNCALL hash-function y))

The ANSI CL standard specifies 4 standard test functions and, under the hood, implementations have the corresponding hash functions.

Define Your Own Test/Hash

Some CL implementations allow you to define your own test-function/hash-function pairs, e.g.,

Your Specific Problem

When the table is string-keyed, I found it extremely profitable to use symbols.

I would create a special package and intern my strings there.

Then I would assign the value to the corresponding symbol: instead of

(defparameter *mytable* (make-hash-table :test 'equal))
(setf (gethash "foo" *mytable*) <whatever>)

I would do

(make-package "MY-PACKAGE")
(setf (symbol-value (intern "foo" "MY-PACKAGE")) <whatever>)

this is especially convenient when the strings are read because CL reader does interning for you.

There are some caveats, of course (e.g., packages have to be explicitly deleted, otherwise they are not GCed), so this is probably not the the faint of heart.

sds
  • 58,617
  • 29
  • 161
  • 278
  • Is defining of my own test/hash functions worth effort? – Mark Karpov Jun 10 '14 at 15:32
  • 1
    probably not. it really depends on _why_ you want to do that - and if performance is the goal, you need to make sure that this is the bottleneck first. – sds Jun 10 '14 at 15:37