FACTOID # 158: 84% of people in Finland feel that they are at a low risk of experiencing a burglary - but just look at how many burglaries they have!
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Algebraic data types

In functional programming, new types can be defined, each of which has one or more constructors. Such a type is known as an algebraic data type. For example, in Haskell we can define a new type, "Tree":

 data Tree = Empty | Leaf Int | Node Tree Tree 

or in Ocaml syntax:

 type tree = Empty | Leaf of int | Node of tree * tree 

with constructors "Empty", "Leaf" and "Node". The constructors can be used much like functions in that they can be (partially) applied to arguments of the appropriate type. For example, the Leaf constructor has the functional type Int -> Tree.


A constructor application cannot be reduced (evaluated) like a function application though since it is already in normal form. Functions which operate on algebraic data types can be defined using pattern matching:

 depth :: Tree -> Int depth Empty = 0 depth (Leaf n) = 1 depth (Node l r) = 1 + max (depth l) (depth r) 

The most common algebraic data type is the list which has constructors Nil and Cons (Cons, the list construction constructor), written in Haskell using the special syntax "[]" for Nil and infix ":" for Cons.


Special cases of algebraic types are product types (only one constructor) and enumeration types (many constructors with no arguments). Algebraic types are one kind of constructed type (i.e. a type formed by combining other types).


An algebraic data type may also be an abstract data type (ADT) if it is exported from a module without its constructors. Objects of such a type can only be manipulated using functions defined in the same module as the type itself.


In set theory the equivalent of an algebraic data type is a discriminated union - a set whose elements consist of a tag (equivalent to a constructor) and an object of a type corresponding to the tag (equivalent to the constructor arguments).

This article was originally based on material from the Free On_line Dictionary of Computing, which is licensed under the GFDL.


  Results from FactBites:
 
Data type - Wikipedia, the free encyclopedia (361 words)
Data type is a type of data in a type system in computer programming.
Types of data in programming languages include primitive types, tuples, records, algebraic data types, abstract data types, reference types, classes, and nodes of data structures.
The smallest addressable unit of data is a group of bits called a byte (usually an octet, which is 8 bits).
Algebraic data type - Wikipedia, the free encyclopedia (727 words)
An algebraic data type is a datatype whose each value is data from other datatypes wrapped in one of the constructors of the datatype.
In set theory the equivalent of an algebraic data type is a discriminated union - a set whose elements consist of a tag (equivalent to a constructor) and an object of a type corresponding to the tag (equivalent to the constructor arguments).
Languages with more expressive type systems based upon something similar to System-Fω (i.e., systems that use kinds, where a kind serves as a "type of a type"), such as Haskell, may however use constructors directly in substitution of a normal function.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m