Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Data Types CSCE 314 Spring 2016. CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.

Similar presentations


Presentation on theme: "More Data Types CSCE 314 Spring 2016. CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for."— Presentation transcript:

1 More Data Types CSCE 314 Spring 2016

2 CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for an existing type (like typedef in C). 2.data – Define a new data type from scratch, by describing its constructors. 3.newtype – A retricted form of data that is more efficient when it fits (if the type has exactly one constructor with exactly one field inside it). Used for defining “wrapper” types. Note: Data types should be capitalized!

3 Spring 2016 CSCE 314 – Programming Studio Type declarations (review) A new name for an existing type can be defined using a type declaration. type String = [Char] Type declarations can be used to make other types easier to read. For example, given type Pos = (Int, Int) Type declarations with parameters type Pair a = (a, a) Data declaration, without or with parameters data Bool = False | True data Shape = Circle Float | Rect Float Float

4 Spring 2016 CSCE 314 – Programming Studio Constructors for data types data Person = Person Name Gender Age type Name = String data Gender = Male | Female type Age = Int With just one constructor in a data type, often constructor is named the same as the type (Person in this case): let x = Person “Harry” Male 13 y = Person “Hermione” Female 13 in …

5 Spring 2016 CSCE 314 – Programming Studio Pattern Matching oldMan (Person _ Male a) | a > 100 = True oldMan (Person _ _ _) = False > let dumbledore = Person “Dumbledore” Male 115 in oldMan dumbledore True

6 Spring 2016 CSCE 314 – Programming Studio The Maybe type A parameterized data type data Maybe a = Nothing | Just a Think of it as representing things of type a that can fail safediv :: Int -> Int -> Maybe Int safediv _0 = Nothing safediv m n = Just (m `div` n) safehead :: [a] -> Maybe a safehead [] = Nothing safehead xs = Just (head xs)

7 Spring 2016 CSCE 314 – Programming Studio Recursive Data types Types declared using type can be nested: type Pos = (Int, Int) type LineSeg = (Pos, Pos) But cannot be recursive type Tree = (Int, [Tree]) -- NO!!! Types declared using data can be recursive!

8 Spring 2016 CSCE 314 – Programming Studio Recursive Data Type Example (defining natural numbers): data Nat = Zero | Succ Nat So, a value of type Nat is either Zero or of the form Succ n where n :: Nat. That is, Nat contains the infinite series of values: Zero Succ Zero Succ (Succ Zero) … Example function: add :: Nat -> Nat -> Nat add Zero n = n add (Succ m) n = Succ (add m n)

9 Spring 2016 CSCE 314 – Programming Studio Example Adding 2 + 1 1 is Succ Zero 2 is Succ (Succ Zero) so, 2 + 1 is add (Succ (Succ Zero)) (Succ Zero) Succ (add (Succ Zero) (Succ Zero)) Succ (Succ (add Zero (Succ Zero))) Succ (Succ (Succ Zero))) which is 3 add :: Nat -> Nat -> Nat add Zero n = n add (Succ m) n = Succ (add m n)

10 Spring 2016 CSCE 314 – Programming Studio Lists as a recursive data type Note, though, that lists have a special syntax in Haskell, with [], etc. data List a = Nil | Cons a (List a) sum :: List Int -> Int sum Nil = 0 sum (Cons x xs) = x + sum xs

11 Spring 2016 CSCE 314 – Programming Studio Trees as a recursive data type A binary Tree is either Tnil, or a Node with a value of type a and two subtrees (of type Tree a ) data Tree a = Tnil | Node a (Tree a) (Tree a) Find an element treeElem :: (a -> Bool) -> Tree a -> Maybe a treeElem p Tnil = Nothing treeElem p t@(Node v left right) | p v = Just v | otherwise = treeElem p left `combine` treeElem p right where combine (Just v) r = Just v combine Nothing r = r

12 Spring 2016 CSCE 314 – Programming Studio Trees as a recursive data type A binary Tree is either Tnil, or a Node with a value of type a and two subtrees (of type Tree a ) data Tree a = Tnil | Node a (Tree a) (Tree a) Depth of list depth Tnil = 0 depth (Node _ left right) = 1 + (max (depth left) (depth right))

13 Spring 2016 CSCE 314 – Programming Studio Example: Arithmetic expressions Consider expressions formed by addition and multiplication: 1 +  32

14 Spring 2016 CSCE 314 – Programming Studio Arithmetic expressions Using recursion, a suitable new type to represent such expressions can be declared by: data Expr = Val Int | Add Expr Expr | Mul Expr Expr For example, the expression on the previous slide would be: Add (Val 1) (Mul (Val 2) (Val 3))

15 Spring 2016 CSCE 314 – Programming Studio Arithmetic expressions Using recursion, it is now easy to define functions that process expressions: size :: Expr  Int size (Val n) = 1 size (Add x y) = size x + size y size (Mul x y) = size x + size y eval :: Expr  Int eval (Val n) = n eval (Add x y) = eval x + eval y eval (Mul x y) = eval x * eval y

16 Spring 2016 CSCE 314 – Programming Studio Folds Folds are of great importance in functional programming, and are not limited to just lists Fold operation for Trees: treeFold :: t -> (a -> t -> t -> t) -> Tree a -> t treeFold f g Tnil = f treeFold f g (Node x l r) = g x (treeFold f g l) (treeFold f g r) Think: replace all Tnil constructors with f, all Node constructors with g

17 Spring 2016 CSCE 314 – Programming Studio Treefold examples > let tt = Node 1 (Node 2 Tnil Tnil) (Node 3 Tnil (Node 4 Tnil Tnil)) > treeFold 1 (\x y z -> 1 + max y z) tt 4 > treeFold 1 (\x y z -> x * y * z) tt 24 > treeFold 0 (\x y z -> x + y + z) tt 10 2 1 3 4


Download ppt "More Data Types CSCE 314 Spring 2016. CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for."

Similar presentations


Ads by Google