Association list explained

Association list
Type:associative array
Space Avg:O(n)
Space Worst:O(n)
Search Avg:O(n)
Search Worst:O(n)
Insert Avg:O(1)
Insert Worst:O(1)
Delete Avg:O(n)
Delete Worst:O(n)

In computer programming and particularly in Lisp, an association list, often referred to as an alist, is a linked list in which each list element (or node) comprises a key and a value. The association list is said to associate the value with the key. In order to find the value associated with a given key, a sequential search is used: each element of the list is searched in turn, starting at the head, until the key is found. Associative lists provide a simple way of implementing an associative array, but are efficient only when the number of keys is very small.

Operation

An associative array is an abstract data type that can be used to maintain a collection of key–value pairs and look up the value associated with a given key. The association list provides a simple way of implementing this data type.

To test whether a key is associated with a value in a given association list, search the list starting at its first node and continuing either until a node containing the key has been found or until the search reaches the end of the list (in which case the key is not present).To add a new key–value pair to an association list, create a new node for that key-value pair, set the node's link to be the previous first element of the association list, and replace the first element of the association list with the new node.[1] Although some implementations of association lists disallow having multiple nodes with the same keys as each other, such duplications are not problematic for this search algorithm: duplicate keys that appear later in the list are ignored.[2]

It is also possible to delete a key from an association list, by scanning the list to find each occurrence of the key and splicing the nodes containing the key out of the list.[1] The scan should continue to the end of the list, even when the key is found, in case the same key may have been inserted multiple times.

Performance

The disadvantage of association lists is that the time to search is, where is the length of the list.[3] For large lists, this may be much slower than the times that can be obtained by representing an associative array as a binary search tree or as a hash table.Additionally, unless the list is regularly pruned to remove elements with duplicate keys, multiple values associated with the same key will increase the size of the list, and thus the time to search, without providing any compensatory advantage.

One advantage of association lists is that a new element can be added in constant time. Additionally, when the number of keys is very small, searching an association list may be more efficient than searching a binary search tree or hash table, because of the greater simplicity of their implementation.[4]

Applications and software libraries

In the early development of Lisp, association lists were used to resolve references to free variables in procedures.[5] [6] In this application, it is convenient to augment association lists with an additional operation, that reverses the addition of a key–value pair without scanning the list for other copies of the same key. In this way, the association list can function as a stack, allowing local variables to temporarily shadow other variables with the same names, without destroying the values of those other variables.[7]

Many programming languages, includingLisp,[5] Scheme,[8] OCaml,[9] andHaskell[10] have functions for handling association lists in their standard libraries.

See also

Notes and References

  1. Book: Programming with Constraints: An Introduction. Kim. Marriott. Peter J.. Stuckey. MIT Press. 1998. 9780262133418. 193–195.
  2. Book: Frické, Martin. Logic and the Organization of Information. Springer. 2012. 9781461430872. 44–45. https://books.google.com/books?id=HnAmoLBcilIC&pg=PA44. 2.8.3 Association Lists.
  3. Book: Knuth, Donald. Donald Knuth. 6.1 Sequential Searching. The Art of Computer Programming, Vol. 3: Sorting and Searching. 396–405. Addison Wesley. 2nd. 0-201-89685-0.
  4. Book: Janes, Calvin. Developer's Guide to Collections in Microsoft .NET. Pearson Education. 2011. 9780735665279. 191. https://books.google.com/books?id=-KdCAwAAQBAJ&pg=PT191. Using Association Lists for Associative Arrays.
  5. Book: LISP 1.5 Programmer's Manual . . John . McCarthy . Paul W. . Abrahams . Daniel J. . Edwards . Timothy P. . Hart . Michael I. . Levin . 0-262-13011-4 . 1985 . registration . See in particular p. 12 for functions that search an association list and use it to substitute symbols in another expression, and p. 103 for the application of association lists in maintaining variable bindings.
  6. Book: van de Snepscheut, Jan L. A.. What Computing Is All About. Monographs in Computer Science. Springer. 1993. 9781461227106. 201.
  7. Book: Scott, Michael Lee. Programming Language Pragmatics. Morgan Kaufmann. 2000. 9781558604421. 137. https://books.google.com/books?id=To3xpkvkPvMC&pg=PA137. 3.3.4 Association Lists and Central Reference Tables.
  8. Book: Pearce, Jon. Programming and Meta-Programming in Scheme. Undergraduate Texts in Computer Science. Springer. 2012. 9781461216827. 214.
  9. Book: Real World OCaml: Functional Programming for the Masses. Yaron. Minsky. Anil. Madhavapeddy. Jason. Hickey. O'Reilly Media. 2013. 9781449324766. 253.
  10. Book: Real World Haskell: Code You Can Believe In. Bryan. O'Sullivan. John. Goerzen. Donald Bruce. Stewart. O'Reilly Media. 2008. 9780596554309. 299.
  11. Web site: 10.1. The Property List. Cs.cmu.edu. 29 September 2017.