Reputation: 1937
I am reading a book about Common LISP and the author says : "An alist consists of key/value pairs stored in a list."
So it made me think, is this the same thing as a Dictionnary in C# or not? If so why?
Upvotes: 3
Views: 1070
Reputation: 9451
There is a distinction between abstract concepts and concrete data-structures.
A dictionary is an abstract concept - a mapping of keys to values. It can be implemented with several different approaches:
C#'s Dictionary is a data-structure that supports (amortized) constant lookup time, like CL's hash-table. In this regard it is different from alist, that has linear lookup time. Also alists don't depend on element's hash-code in order to store/retrieve it. The API of alists is also different, than Dictionary's. There's assoc
and rassoc
to lookup an element from the left side and right side respectively. (So, unlike in a conventional dictionary, there can be multiple mappings of the same key to different values in an alist). Also there are acons
and pairlis
to construct an alist
.
EDIT And, finally, there's no standard function to remove an item from alist.
You can delete an element with (remove key alist :key #'car)
. But note, that this operation leaves the original list intact and returns a modified list as a result.
Upvotes: 8
Reputation: 223133
Comparisons between alists and hash tables (which Dictionary
uses), since nobody seems to have touched on this yet:
Alists:
Hash tables:
Upvotes: 2
Reputation: 15810
Yes, they are very similar and serve a similar purpose, but there is at least one significant difference:
A .NET Dictionary
cannot have multiple items with the same key!
Apparently, an alist can:
When searching an association list for an association with a given key, the first one found is returned, if there is more than one.
Upvotes: 0
Reputation: 139311
It's a mapping from keys to values. So it can act similar to a dictionary. During the evolution of Lisp, hash-tables have been added, which serve a similar purpose.
Upvotes: 0
Reputation: 12883
Yes, its pretty much the same thing. Also called associative arrays (perl) and maps (java and others).
Upvotes: -1