Jump to content

Algebraic data types: Difference between revisions

m
copy edit - wiki link to wikipedia
m (moved Pattern Matching to Pattern matching over redirect)
m (copy edit - wiki link to wikipedia)
Line 1:
{{task|Data Structures}}
 
Some languages offer direct support for [http[wp://en.wikipedia.org/wiki/Algebraic_data_type |algebraic data types]] and pattern matching on them. While this of course can always be simulated with manual tagging and conditionals, it allows for terse code which is easy to read, and can represent the algorithm directly.
 
As an example, implement insertion in a [http[wp://en.wikipedia.org/wiki/Red_Black_Tree |red-black-tree]]. A red-black-tree is a binary tree where each internal node has a color attribute ''red'' or ''black''. Moreover, no red node can have a red child, and every path from the root to an empty node must contain the same number of black nodes. As a consequence, the tree is balanced, and must be re-balanced after an insertion.
 
=={{header|Common Lisp}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.