Chapter 2 A Module of Shapes: Part I. Defining New Datatypes  The ability to define new data types in a programming language is important.  Kinds of.

Slides:



Advertisements
Similar presentations
Modelling & Datatypes John Hughes. Software Software = Programs + Data.
Advertisements

Modelling & Datatypes Koen Lindström Claessen. Software Software = Programs + Data.
A Fourth Look At ML 1. Type Definitions Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML datatype bool = true | false;
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Haskell Chapter 7. Topics  Defining data types  Exporting types  Type parameters  Derived instances  Type synonyms  Either  Type classes  Not.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Chapter 8 A Module of Regions. The Region Data Type  A region represents an area on the two-dimensional Cartesian plane.  It is represented by a tree-like.
Chapter 6 Shapes III: Perimeters of Shapes. The Perimeter of a Shape  To compute the perimeter we need a function with four equations (1 for each Shape.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A Fourth Look At ML Chapter ElevenModern Programming Languages, 2nd ed.1.
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
Cse536 Functional Programming 1 6/10/2015 Lecture #8, Oct. 20, 2004 Todays Topics –Sets and characteristic functions –Regions –Is a point in a Region –Currying.
Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.
Advanced Programming Handout 4. Introductions  Me: Benjamin C. Pierce (known as Benjamin, or, if you prefer, Dr. Pierce, but not Ben or Professor) 
Chapter 1 Problem Solving, Programming, and Calculation.
Cse536 Functional Programming 1 6/28/2015 Lecture #3, Oct 4, 2004 Reading Assignments –Finish chapter 2 and begin Reading chapter 3 of the Text Today’s.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
Shapes and Patterns 1 st Grade ELL SHAPES What shape am I? I am a square.
Defining Classes Modules and ADTs CSCE 314 Spring 2016.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
5 Day Forecast Mon Tues Wed Thu Fri.
Theory of Computation Lecture 4: Programs and Computable Functions II
Pronouns and Antecedents Chapter 5 Pronouns and Antecedents Chapter 5
Lecture 16: Introduction to Data Types
GANTT CHARTS Example Example Example Example text Tasks Example 1
Mon – 2Tue – 3Wed – 4Thu - 5Fri - 6Sat - 7Sun - 8 Mon – 9Tue – 10Wed – 11Thu - 12Fri – 13Sat – 14Sun -15 Mon – 16Tue – 17Wed – 18Thu - 19Fri – 20Sat –
MON TUE WED THU
1   1.テキストの入れ替え テキストを自由に入れ替えることができます。 フチなし全面印刷がおすすめです。 印刷のポイント.
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
January Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
Shapes.
Types and Classes in Haskell
Pattern Matching Pattern matching allows us to do things like this:
2008 Calendar.
S M T W F S M T W F
January MON TUE WED THU FRI SAT SUN
Sun Mon Tue Wed Thu Fri Sat
2 0 X X s c h e d u l e 1 MON TUE WED THU JANUARY 20XX FRI SAT SUN MEMO.
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Calendar
Calendar – 2010 (October, November & December)
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Theory of Computation Lecture 4: Programs and Computable Functions II
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
January MON TUE WED THU FRI SAT SUN
S M T W F S M T W F
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Can you work out the next shape in the pattern?
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
Review Previously User-defined data types: records, variants
1 January MON TUE WED THU FRI SAT SUN MEMO 2 February MON TUE WED THU FRI SAT SUN.
2008 Calendar.
1 January MON TUE WED THU FRI SAT SUN MEMO 2 February MON TUE WED THU FRI SAT SUN.
Presentation transcript:

Chapter 2 A Module of Shapes: Part I

Defining New Datatypes  The ability to define new data types in a programming language is important.  Kinds of data types: enumerated types records (or products) variant records (or sums) recursive types  Haskell’s data declaration provides these kinds of data types in a uniform way that abstracts away from their implementation details, by providing an abstract interface to the newly defined type.  Before looking at the example from Chapter 2, let’s look at some simpler examples.

The Data Declaration  Example of an enumeration data type: data Day = Sun | Mon | Tue | Wed | Thu | Fri | Sat deriving Show  The names Sun through Sat are constructor constants (since they have no arguments) and are the only elements of the type.  For example, we can define: valday :: Integer -> Day valday 1 = Sun valday 2 = Mon valday 3 = Tue valday 4 = Wed valday 5 = Thu valday 6 = Fri valday 7 = Sat Hugs> valday 4 Wed

Constructors & Patterns  Constructors can be matched by patterns.  For example : dayval :: Day -> Integer dayval Sun = 1 dayval Mon = 2 dayval Tue = 3 dayval Wed = 4 dayval Thu = 5 dayval Fri = 6 dayval Sat = 7 Hugs> dayval Wed 4

Other Enumeration Data Type Examples data Bool = True | False -- predefined in Haskell deriving Show data Direction = North | East | South | West deriving Show data Move = Paper | Rock | Scissors deriving Show beats :: Move -> Move beats Paper = Scissors beats Rock = Paper beats Scissors = Rock Hugs> beats Paper Scissors

Variant Records  More complicated data types: data Tagger = Tagn Integer | Tagb Bool  These constructors are not constants – they are functions: Tagn :: Integer -> Tagger Tagb :: Bool -> Tagger  As for all constructors, something like “ Tagn 12 ” Cannot be simplified (and thus, as discussed in Chapter 1, it is a value). Can be used in patterns.

Example functions on Tagger number (Tagn n) = n boolean (Tagb b) = b isNum (Tagn _) = True isNum (Tagb _) = False isBool x = not (isNum x) Hugs> :t number number :: Tagger -> Integer Hugs> number (Tagn 3) 3 Hugs> isNum (Tagb False) False

Another Variant Record Data Type data Temp = Celsius Float | Fahrenheit Float | Kelvin Float  We can use patterns to define functions over this type: toKelvin (Celsius c) = Kelvin (c ) toKelvin (Fahrenheit f) = Kelvin ( 5/9*(f-32.0) ) toKelvin (Kelvin k) = Kelvin k

Finally: the Shape Data Type from the Text  The Shape data type from Chapter 2 is another example of a variant data type: data Shape = Rectangle Float Float | Ellipse Float Float | RtTriangle Float Float | Polygon [(Float,Float)] deriving Show  The last line – “ deriving Show ” – tells the system to build a show function for the type Shape (more on this later).  We can also define functions yielding refined shapes: circle, square :: Float -> Shape circle radius = Ellipse radius radius square side = Rectangle side side

Functions over shape  Functions on shapes can be defined using pattern matching. area :: Shape -> Float area (Rectangle s1 s2) = s1*s2 area (Ellipse r1 r2) = pi*r1*r2 area (RtTriangle s1 s2) = (s1*s2)/2 area (Polygon (v1:pts)) = polyArea pts where polyArea :: [(Float,Float)] -> Float polyArea (v2:v3:vs) = triArea v1 v2 v3 + polyArea (v3:vs) polyArea _ = 0 Note use of auxiliary function. Note use of nested patterns. Note use of wild card pattern (which matches anything).

A B C D E F totalArea = area (triangle [A,B,C]) + area (polygon[A,C,D,E,F]) Algorithm for Computing Area of Polygon

TriArea triArea v1 v2 v3 = let a = distBetween v1 v2 b = distBetween v2 v3 c = distBetween v3 v1 s = 0.5*(a+b+c) in sqrt (s*(s-a)*(s-b)*(s-c)) distBetween (x1,y1) (x2,y2) = sqrt ((x1-x2)^2 + (y1-y2)^2)