4 ms·
But why did Common Lisp need to build in at least 3 separate ways to map keys to values (getf, assoc, and hash tables)? The answer is because the language desig
by omouse 18y ago
But why did Common Lisp need to build in at least 3 separate ways to map keys to values (getf, assoc, and hash tables)? The answer is because the language designers wanted all three.
Actually, the answer is that those three do different things. GETF is for property lists which look like '(key1 value1 key2 value2), ASSOC is for association lists which look like '((key1 . value1) (key2 . value2)), and GETHASH is for a hashtable which looks like a vector/array and not a list.
GETF returns the value of the first key that matches. GETHASH will return the value of the hashed key that matches.
ASSOC allows you to return any value for whatever key you're getting or whatever test you're using.
So each one has a different purpose. The trade-off is how you choose to map keys to values. It may be faster to use a hash-table instead of an assoc-list, or it may be easier to interact with an assoc-list rather than a property list. The point is that there's a choice and a reason why the choice is available.
This reminds me of all the newbie Java programmers who use the ArrayList class instead of looking for just the right data structure for what they want to do.
- jrockway 18y agoMy complaint was having all of these in the core language. If I want to use property lists, why can't I just load the "plist" library? If I want association lists, why can't I just load the "alist" library? And so on... the core programming language is not the place to dump every algorithm ever created.