Defining new types of data Defining New Datatypes






![Constructors with arguments (sometimes called sums or variants) data [a] = [] | (: Constructors with arguments (sometimes called sums or variants) data [a] = [] | (:](https://slidetodoc.com/presentation_image_h2/bfc50d6f919324537a3993d272acaa04/image-7.jpg)









- Slides: 16
Defining new types of data
Defining New Datatypes • Ability to add new datatypes in a programming language is important. • Kinds of datatypes enumerated types records (or products or struct) variant records (or sums) pointer types arrays • Haskell’s data declaration provides many of these kinds of – – – types in a uniform way which abstracts from their implementation details. • The data declaration defines new functions and constants, which provide an abstract interface to the newly defined type.
The Data Declaration • Enumeration Types data Day = Sun | Mon | Tue | Wed | Thu | Fri | Sat deriving Eq • The names on right hand side are constructor constants and are the only elements of the type.
Other enumeration data types we already know • data Ordering = Eq | LT | GT • data Bool = True | False
Writing functions (1) • Functions written by mult-clause definitions day. Of. Week: : Day day. Of. Week Sun = day. Of. Week Mon = day. Of. Week Tue = dya. Of. Week Wed = day. Of. Week Thu = day. Of. Week Fri = day. Of. Week Sat = ? day. Of. Week Thu 5 : : Int -> Int 1 2 3 4 5 6 7
Writing functions (2) • Functions written by using the case expression day. Of. Week: : Day -> Int day. Of. Week x = case x of Sun -> 1 Mon -> 2 Tue -> 3 Wed -> 4 Thu -> 5 Fri -> 6 Sat -> 7
Constructors with arguments (sometimes called sums or variants) data [a] = [] | (: ) a [a] data Tree a = Tip | Fork (Tree a) a (Tree a)
Constructors & Patterns • data defined types define new constructors and can be accessed by patterns. • Constructors without arguments are constants • Example using case day. Of. Week x = case x of Sun -> 0 Mon -> 1 Tue -> 2 Wed -> 3 ; Thu -> 4 ; Fri -> 5 Sat -> 6 • Note: Indentation bounds each clause ( pat -> body) in case, or use a semi-colon rather than indentation.
Patterns in Declarations • In a declaration patterns can be used. Possible to have many lines for a definition if each pattern is distinct. day. Of. Week day. Of. Week Sun Mon Tue Wed Thu Fri Sat = = = = ? day. Of. Week Tue 2 0 1 2 3 4 5 6
Patterns with variables • If the constructors aren’t constants then patterns have variables len [] = 0 len (x : xs) = 1 + len xs depth Tip = 0 depth (Fork x a y) = 1 + max (depth x) (depth y)
Patterns in case expressions • One can use patterns in case expressions as well as in multiclause defintions len x = case x of [] -> 0 (x : xs) -> 1 + len xs depth x = case x of Tip -> 0 (Fork x a y) -> 1 + max (depth x) (depth y) Note use of indentation
Rules for patterns • When writing functions (using multi clause functions or case expressions) there is always 1 case for each of the construtors. • Patterns always have constructors and variables. – Constructors start with a capital letter • Exceptions [] and cons (: ) which are special syntax for lists, and (x, y, z) which is special syntax for tuples. • [x, y, z] syntax can also be used for lists of fixed length! – Constants don’t have variables – Constructors with args must always have the correct number • Patterns can be nested – – (Fork Tip x Tip) [(x, y, z)]
Other Enumeration Examples data Move = Paper | Rock | Scissors beats : : Move -> Move Paper = Scissors Rock = Paper Scissors = Rock ? beats Paper Scissors data Bool = True | False data Direction = North | East | South | West
More about Variant Records • More complicated types data Tagger = Tagn Int | Tagb Bool • NOTE: the types of the constructors are functions (not constants as for enumerated types) Tagb : : Bool -> Tagger Tagn : : Int -> Tagger • As for all constructors: (Tagn 12) • 1) Cannot be simplified. We say it is Canonical • 2) Can be used in a pattern on the left hand side of an =
Example functions on Tagger number (Tagn n) = n boolean (Tagb b) = b is. Num (Tagn _) = True is. Num (Tagb _) = False ? : t number : : Tagger -> Int ? number (Tagn 3) 3 ? is. Num (Tagb False) False
Another Variant Record-like Type data Temp = Celsius Float | Fahrenheit Float | Kelvin Float • Use patterns to define functions over this type: to. Kelvin (Celsius c) = Kelvin(c + 273. 0) to. Kelvin (Fahrenheit f) = Kelvin( (f - 32. 0) * (5. 0/9. 0) + 273. 0 ) to. Kelvin (Kelvin k) = Kelvin k Note: 3 constructors means 3 clauses