Algebraic data types: Difference between revisions

Content added Content deleted
m (moved Pattern Matching to Pattern matching over redirect)
m (copy edit - wiki link to wikipedia)
Line 1: Line 1:
{{task|Data Structures}}
{{task|Data Structures}}


Some languages offer direct support for [http://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.
Some languages offer direct support for [[wp: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://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.
As an example, implement insertion in a [[wp: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}}==
=={{header|Common Lisp}}==