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.

Slides:



Advertisements
Similar presentations
Functional Programming Lecture 10 - type checking.
Advertisements

0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
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.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Advanced Programming Handout 9 Qualified Types (SOE Chapter 12)
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 –
Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Lecture notes, exercises, etc., can be found at:
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
Chapter 12 Qualified Types. Motivation  What should the principal type of (+) be? Int -> Int -> Int-- too specific a -> a -> a-- too general  It seems.
Data Types. Every program must deal with data The data is usually described as a certain type This type determines what you can do with the data and how.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Lecture #5 Introduction to C++
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Control statements Mostafa Abdallah
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Data in Haskell. What is data? Measurements of some kind stored in a computer. – Examples Simple: numbers, text, truth values … Structured: sequences,
Guards1. 2 Let’s test this function. Main> maxi Here is a trace of the function: n m maxi 3 2 Guards or conditions are used to express various cases.
1 PROGRAMMING IN HASKELL Lecture 2 Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Polymorphic Functions
String is a synonym for the type [Char].
Programming Languages
PROGRAMMING IN HASKELL
Types CSCE 314 Spring 2016.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Haskell Chapter 2.
Lecture 2:Data Types, Functional Patterns, Recursion, Polymorphism
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
Data Types, Identifiers, and Expressions
CSE 3302 Programming Languages
Chapter 2.
Arrays, For loop While loop Do while loop
PROGRAMMING IN HASKELL
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Programming Languages
Selection CSCE 121 J. Michael Moore.
Introduction to Primitive Data types
Chapter 2: Java Fundamentals
Local Variables, Global Variables and Variable Scope
Type & Typeclass Syntax in function
PROGRAMMING IN HASKELL
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Variables and Constants
Introduction to Primitive Data types
Presentation transcript:

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 evaluate that expression In Haskell, read “::” as “has type” Examples: – 1 :: Int, 'a' :: Char, True :: Bool, 1.2 :: Float, … You can even ask GHCI for the type of an expression: :t expr

3 Type Errors: Hugs> 'a' && True ERROR - Type error in application *** Expression : 'a' && True *** Term : 'a' *** Type : Char *** Does not match : Bool Hugs> odd ERROR - Cannot infer instance *** Instance : Num Bool *** Expression : odd Hugs>

4 Pairs: A pair packages two values into one (1, 2) ('a', 'z')(True, False) Components can have different types (1, 'z') ('a', False)(True, 2) The type of a pair whose first component is of type A and second component is of type B is written (A,B) What are the types of the pairs above?

5 Operating on Pairs: There are built-in functions for extracting the first and second component of a pair: – fst (True, 2) = True – snd (0, 7) = 7 Is the following property true? For any pair p, (fst p, snd p) = p

6 Lists: Lists can be used to store zero or more elements, in sequence, in a single value: [] [1, 2, 3] ['a', 'z'] [True, True, False] All of the elements in a list must have the same type The type of a list whose elements are of type A is written as [A] What are the types of the lists above?

Integer Constants like 5, 35, 897 are in the Num class They default to the type Integer

Double Constants like 5.6, and 0.0 are Fractional These default to the type Double

Type declarations If you have a problem with a numeric constant like 5 or 78.9, you will probably see an error that mentions Num or Fractional. Fix these by adding type declarations