Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Lisp: copy editing and linking
Line 721:
</syntaxhighlight>
 
=== [[Lisp programming language|Lisp]] ===
[[Lisp programming language|Lisp]] was originally conceived as a "LISt Processing" language, and one of its most important data types is the linked list, which can be treated as an [[association list]] ("alist").
 
<syntaxhighlight lang=Lisp>
Line 730:
</syntaxhighlight>
 
The syntax <code>(x . y)</code> is used to indicate a [[cons|<code>cons</code>ed]] pair. Keys and values need not be the same type within an alist. Lisp and [[Scheme (programming language)|Scheme]] provide operators such as <code>assoc</code> to manipulate alists in ways similar to associative arrays.
 
A set of operations specific to the handling of association lists exists for [[Common Lisp]], each of these working non-destructively.
 
To add an entry the <code>acons</code> function is employed, creating and returning a new association list. An association list in Common Lisp mimicks a stack, that is, adheres to the last-in-first-out (LIFO) principle, and hence prepends to the list head.
Line 743:
</syntaxhighlight>
 
This function can actually be construed as a convenience accommodatingaccommodation for <code>cons</code> operations.<ref>[https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node153.html]</ref>
 
<syntaxhighlight lang=Lisp>
Line 760:
</syntaxhighlight>
 
Searching for an entry by its key is performed via <code>assoc</code>, which might be configured regarding the test predicate and direction, especially searching from the association list from its end to its front. The result, if positive, constitutesreturns the entire entry cons, not only its value. Failure to obtain a matching key leds to a return of the <code>NIL</code> value.
 
<syntaxhighlight lang=Lisp>