Introduction to Programming in ATS: | ||
---|---|---|
<<< Previous | Next >>> |
A datatype is like a tagged union type. For each datatype, there are some constructors associated with it, and these constructors are needed for constructing values of the datatype. As an example, the following syntax declares a datatype named intopt:
There are two constructors associated with intopt: intopt_none, which is nullary, and intopt_some, which is unary. For instance, intopt_none() and intopt_some(1) are two values of the type intopt. In order for accessing components in such values, a mechanism often referred to as pattern-matching is provided in ATS. I will demonstrate through examples that datatypes plus pattern matching can offer not only great convenience in programming but also clarity in code.The code employed for illustration in this chapter plus some additional code for testing is available on-line.
Patterns in ATS can be defined inductively as follows:
Certain constant values such booleans, chars, floating point numbers, integers and strings are patterns.
The void value () is a pattern.
The underscore symbol _ represents a special wildcard pattern.
Variables are patterns.
A tuple of patterns, either boxed or unboxed, is a pattern.
A record of patterns, either boxed or unboxed, is a pattern.
Given a constructor C, a pattern can be formed by applying C to a given list of patterns.
Given a variable x and a pattern pat, (x as pat) is a referenced pattern, where as is a keyword.
Some other forms of patterns will be introduced elsewhere.
<<< Previous | Home | Next >>> |
Currying and Uncurrying | Up | Pattern-Matching |