Class hierarchies can be extended with new subclasses which implement the same interface, while the functionality of ADTs can be extended for the fixed set of constructors. Thus the extensibility of ADTs is orthogonal to the extensibility of class hierarchies. However, as opposed to class hierarchies, ADTs are closed. In object-oriented programming languages, a disjoint union can be expressed as class hierarchies. They are easy to define and easy to use, largely because of pattern matching as well as most Standard ML implementations' pattern-exhaustiveness checking and pattern redundancy checking. A datatype can be thought of as a disjoint union of tuples (or a "sum of products"). Standard ML provides strong support for algebraic datatypes (ADT). sqrt ( s * ( s - x ) * ( s - y ) * ( s - z )) end Algebraic datatypes sqrt ( square ( x' - x ) + square ( y' - y )) fun heron ( a, b, c ) = let val x = dist a b val y = dist b c val z = dist a c val s = ( x + y + z ) / 2.0 in Math. Type loc = real * real fun square ( x : real ) = x * x fun dist ( x, y ) ( x', y' ) = Math.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |