Modelling & Datatypes Koen Lindström Claessen. Software Software = Programs + Data.

Slides:



Advertisements
Similar presentations
Natalia Sivackova. Events that cannot happen together (e.g. cant a card that is both red and black in a card deck) P(A B) = P(A) + P(B)
Advertisements

Modelling & Datatypes John Hughes. Software Software = Programs + Data.
Combinations Examples
Please Sit... You do not need materials at this time.
Probability: Mutually Exclusive Events 1. There are 3 red, 4 black and 5 blue cubes in a bag. A cube is selected at random. What is the probability of.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
© Glenn Rowe AC Lab 2 A simple card game (twenty- one)
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Mock Objects. Unit-testing and TDD are challenging They require some effort to: – Write a test for a small functionality – Refactor production code and.
CS 116 Tutorial 6 Strings, Raw Input, Lists. 1. Write a function convert_format that consumes nothing, but takes keyboard input. The program prompts "Enter.
Solving Equations A Solution
Whiteboardmaths.com © 2004 All rights reserved
Counting Techniques 1. Sequential Counting Principle Section
Index Student Activity 1: Questions to familiarise students with the
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
7 Quarter 1: Killer Bee Review. 1. Choose the set(s) of numbers to which –100 belongs. 4.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
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.
Test Data Generators Lecture 5. Why Distinguish Instructions? Functions always give the same result for the same arguments Instructions can behave differently.
Modelling & Datatypes Koen Lindström Claessen. Software Software = Programs + Data.
Fixing Broken Programs. How do you figure out what’s wrong? Look carefully at the error message. Locate the error – Most messages have line numbers –
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
UNR, MATH/STAT 352, Spring Radar target detection How reliable is the signal on the screen? (Is it a target of a false alarm?)
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.
Refreshing Your Skills for Chapter 10.  If you flip a coin, the probability that it lands with heads up is 1/2.  If you roll a standard die, the probability.
By Ruben Gonzalez. Playing cards are flat, rectangular pieces of layered pasteboard typically used for playing a variety of games of skill or chance.
Combinations Problems Problem 1: Sometimes we can use several counting techniques in the same problem, such as combinations and the addition principle.
Compound Probability Pre-AP Geometry. Compound Events are made up of two or more simple events. I. Compound Events may be: A) Independent events - when.
Pattern matching. The if expression The else part of an if expression is optional if ( condition ) expression1 else expression2 If the condition evaluates.
Copyright 2013, 2010, 2007, Pearson, Education, Inc. Section 12.2 Theoretical Probability
Chapter  Determine how many different possibilities are possible:  1. There are 3 different ice cream flavors and 5 different toppings. You.
15.3 Counting Methods: Combinations ©2002 by R. Villar All Rights Reserved.
Introduction to Computer Science 2 Slide 1 Enumerated types We already know some data types int, float, char good for problems that involve int,
1 Functional Programming Lecture 6 - Algebraic Data Types.
Today’s Objectives:. Warm Up TOC WWK Mutually Exclusive & Mutually Exclusive—two events are mutually exclusive if they CANNOT occur at the same.
16. STRUCTURES, UNIONS, AND ENUMERATIONS. Declaring Structures A structure is a collection of one or more components (members), which may be of different.
HAWKES LEARNING Students Count. Success Matters. Copyright © 2015 by Hawkes Learning/Quant Systems, Inc. All rights reserved. Section 2.3 Operations with.
Draw 3 cards without replacement from a standard 52 card deck. What is the probability that: 1.They are all red ? 2.At least one is black ? 3.They are.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Lesson Objective: Understand how to make and use Algorithms Learning Outcome: Design your own algorithms and flowcharts. Algorithms and Flowcharts.
0-11 Probability Goal: Find the probability of an event occurring. Eligible Content: A
Chapter 8: Arrays Gator Engineering One-dimensional array Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Move the first element to the.
Some Practical Information + Programming with Lists Sept Koen Lindström Claessen.
Test Data Generators. Why Distinguish Instructions? Functions always give the same result for the same arguments Instructions can behave differently on.
Some Practical Information + Programming with Lists
Combinations Problems
Koen Lindström Claessen
Chapter 3 Practice.
The Addition Rule.
Chapter 5 Black Jack.
Section 2 – CSE341 Patrick Larson, Spring 2013.
Test Data Generators.
Section 2 – CSE341 Konstantin Weitz.
Nicholas Shahan Spring 2016
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
CSE 341 PL Section 2 Justin Harjanto.
Shapes.
Spencer Pearson Spring 2017
Strong’s GMAS Review, Week 5, Problems 4 - 5
CSE 341 Section 2 Nick Mooney Spring 2017
The meaning of probability
Notes Over 9.1 Finding Square Roots of Numbers
Section 12.2 Theoretical Probability
Section 12.2 Theoretical Probability
BEGINNERS’ LESSONS Welcome
Homework Due Friday.
Can you work out the next shape in the pattern?
Section 12.2 Theoretical Probability
Can you work out the next shape in the pattern?
Presentation transcript:

Modelling & Datatypes Koen Lindström Claessen

Software Software = Programs + Data

Modelling Data A big part of designing software is modelling the data in an appropriate way Numbers are not good for this! We model the data by defining new types

Modelling a Card Game Every card has a suit Model by a new type: data Suit = Spades | Hearts | Diamonds | Clubs The new type The values of this type Hearts, Whist, Plump, Bridge,...

Investigating the new type Main> :i Suit -- type constructor data Suit -- constructors: Spades :: Suit Hearts :: Suit Diamonds :: Suit Clubs :: Suit Main> :i Spades Spades :: Suit -- data constructor The new type The new values -- constructors Types and constructors start with a capital letter

Printing Values Fix Main> Spades ERROR - Cannot find "show" function for: *** Expression : Spades *** Of type : Suit Main> :i show show :: Show a => a -> String -- class member Needed to print values data Suit = Spades | Hearts | Diamonds | Clubs deriving Show Main> Spades Spades

The Colours of Cards Each suit has a colour – red or black Model colours by a type Define functions by pattern matching data Colour = Black | Red deriving Show colour :: Suit -> Colour colour Spades = Black colour Hearts = Red colour Diamonds = Red colour Clubs = Black One equation per value Main> colour Hearts Red

The Ranks of Cards Cards have ranks: 2..10, J, Q, K, A Model by a new type Numeric ranks data Rank = Numeric Integer | Jack | Queen | King | Ace deriving Show Main> :i Numeric Numeric :: Integer -> Rank -- data constructor Main> Numeric 3 Numeric 3 Numeric ranks contain an Integer

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank rankBeats :: Rank -> Rank -> Bool

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False Matches anything at all Nothing beats an Ace

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True Used only if the first equation does not match. An Ace beats anything else

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True rankBeats _ King = False rankBeats King _ = True

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True rankBeats _ King = False rankBeats King _ = True rankBeats _ Queen = False rankBeats Queen _ = True rankBeats _ Jack = False rankBeats Jack _ = True

Rank Beats Rank When does one rank beat another? AKQJmAKQJm nJQKAnJQKA m>n

Rank Beats Rank rankBeats :: Rank -> Rank -> Bool rankBeats _ Ace = False rankBeats Ace _ = True rankBeats _ King = False rankBeats King _ = True rankBeats _ Queen = False rankBeats Queen _ = True rankBeats _ Jack = False rankBeats Jack _ = True rankBeats (Numeric m) (Numeric n) = m > n Match Numeric 7, for example Names the number in the rank

Examples Main> rankBeats Jack (Numeric 7) True Main> rankBeats (Numeric 10) Queen False

Modelling a Card A Card has both a Rank and a Suit Define functions to inspect both data Card = Card Rank Suit deriving Show rank :: Card -> Rank rank (Card r s) = r suit :: Card -> Suit suit (Card r s) = s

A Useful Abbreviation Define type and inspection functions together, as follows data Card = Card {rank :: Rank, suit :: Suit} deriving Show

When does one card beat another? When both cards have the same suit, and the rank is higher cardBeats :: Card -> Card -> Bool cardBeats c c' | suit c == suit c' = rankBeats (rank c) (rank c') | otherwise = False data Suit = Spades | Hearts | Diamonds | Clubs deriving (Show, Eq) can be written down simpler...

When does one card beat another? When both cards have the same suit, and the rank is higher cardBeats :: Card -> Card -> Bool cardBeats c c' = suit c == suit c && rankBeats (rank c) (rank c')

Intermezzo: Figures Modelling geometrical figures –triangle –rectangle –circle data Figure = Triangle... | Rectangle... | Circle... circumference :: Figure -> Double circumference =...

Intermezzo: Figures data Figure = Triangle Double Double Double | Rectangle Double Double | Circle Double circumference :: Figure -> Double circumference (Triangle a b c) = a + b + c circumference (Rectangle x y) = 2* (x + y) circumference (Circle r) = 2 * pi * r

Intermezzo: Figures data Figure = Triangle Double Double Double | Rectangle Double Double | Circle Double -- types Triangle :: Double -> Double -> Double -> Figure Rectangle :: Double -> Double -> Figure Circle :: Double -> Figure square :: Double -> Figure square s = Rectangle s s

Modelling a Hand of Cards A hand may contain any number of cards from zero up! The solution is… recursion! data Hand = Cards Card … Card deriving Show We cant use …!!!

Modelling a Hand of Cards A hand may contain any number of cards from zero up! –A hand may be empty –It may consist of a first card and the rest The rest is another hand of cards! data Hand = Empty | Add Card Hand deriving Show A recursive type! Solve the problem of modelling a hand with one fewer cards! very much like a list...

When can a hand beat a card? An empty hand beats nothing A non-empty hand can beat a card if the first card can, or the rest of the hand can! A recursive function! handBeats :: Hand -> Card -> Bool handBeats Empty card = False handBeats (Add c h) card = cardBeats c card || handBeats h card

What Did We Learn? Modelling the problem using datatypes with components Using recursive datatypes to model things of varying size Using recursive functions to manipulate recursive datatypes Writing properties of more complex algorithms