Association list

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Association list
Type associative array
Time complexity
in big O notation
Average Worst case
Space O(n) O(n)
Search O(n) O(n)
Insert O(1) O(1)
Delete O(n) 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, each element of the list is searched in turn, starting at the head, until the key is found. Duplicate keys that appear later in the list are ignored. It is a simple way of implementing an associative array.

The disadvantage of association lists is that the time to search is O(n), where n is the length of the list. And 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 is that a new element can be added to the list at its head, which can be done in constant time. For quite small values of n it is more efficient in terms of time and space than more sophisticated strategies such as hash tables and trees.

In the early development of Lisp, association lists were used to resolve references to free variables in procedures.[1]

Many programming languages, including Lisp, Scheme, OCaml, and Haskell have functions for handling association lists in their standard library.

In HTML[edit]

The markup language HTML and its derivative XHTML have an analogous set of association list elements, more often referred to as description lists,[2] and known as definition lists prior to HTML5.[3] The <dl>...</dl> element delimits the list, the term (key) is delimited by <dt>...</dt> element, and the description or definition (value) is delimited by the <dd> element. The <dt> and <dd> elements are sister elements, under (inside) the <dl> node. Any given term may be followed by multiple descriptions/definitions, and any of the latter may be preceded by more than one of the former (e.g., alternative spellings of or names for the term). A <dt> does not require a <dd> at all (i.e., may be undefined); a null <dd /> can be used as an empty placeholder for later association. A <dd> with no <dt> (or only a null <dt />) with which to associate is not technically invalid markup, but is semantically poorly formed in most contexts. No other elements may be directly under the <dl> node. Basic flow content nodes (elements), such as italicization and links, may be used within a term node. The description node may contain any flow content at all, including sectioning and heading elements.

References[edit]

  1. ^ McCarthy, John; Abrahams, Paul W.; Edwards, Daniel J.; Hart, Timothy P.; Levin, Michael I. (1985). LISP 1.5 Programmer's Manual (PDF). MIT Press. ISBN 0-262-13011-4. 
  2. ^ "4.5 Grouping content – HTML5". HTML5: A vocabulary and associated APIs for HTML and XHTML – W3C Recommendation. World Wide Web Consortium. 28 October 2014. 4.4.8 The dl element. Retrieved 22 May 2013. 
  3. ^ "Lists in HTML documents". HTML 4.01 Specification – W3C Recommendation. World Wide Web Consortium. 24 December 1999. 10.3 Definition lists: the DL, DT, and DD elements. Retrieved 2 May 2015.