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.

Slides:



Advertisements
Similar presentations
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
Advertisements

Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
String is a synonym for the type [Char].
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A Fourth Look At ML Chapter ElevenModern Programming Languages, 2nd ed.1.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
0 LECTURE 5 LIST COMPREHENSIONS Graham Hutton University of Nottingham.
Com Functional Programming Algebraic Data Types Marian Gheorghe Lecture 12 Module homepage Mole & ©University of.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
Chapter 5 Polymorphic and Higher-Order Functions.
Advanced Programming Handout 5 Recursive Data Types (SOE Chapter 7)
Using Types Slides thanks to Mark Jones. 2 Expressions Have Types: The type of an expression tells you what kind of value you might expect to see if you.
CSE 341 Lecture 10 more about data types; nullable types; option Ullman ; slides created by Marty Stepp
© M. Winter COSC 4P41 – Functional Programming Enumerated types data Temp = Cold | Hot data Season= Spring | Summer | Autumn | Winter weather ::
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
GADTs GADTs in Haskell. ADT vs GADT Algebraic Datatype Data List a = Nil | Cons a (List a) Data Tree a b = Tip a | Node (Tree a b) b | Fork (Tree a b)
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Modules.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
0 Functors in Haskell Adapted from material by Miran Lipovaca.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
1 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Type declarations.
Haskell Chapter 7.
String is a synonym for the type [Char].
ML: a quasi-functional language with strong typing
Theory of Computation Lecture 4: Programs and Computable Functions II
A lightening tour in 45 minutes
Expanded Recursive Diagrams OCAML rapid tour, day 2
PROGRAMMING IN HASKELL
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Types and Classes in Haskell
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
PROGRAMMING IN HASKELL
Trees Today’s Topics Trees Kinds of trees - branching factor
Functions, Patterns and Datatypes
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Review Previously User-defined data types: records, variants
Presentation transcript:

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 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!

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

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 …

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

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)

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!

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)

Spring 2016 CSCE 314 – Programming Studio Example Adding is Succ Zero 2 is Succ (Succ Zero) so, 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)

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

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 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

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))

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

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))

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

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

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