Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can.

Slides:



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

Comp 205: Comparative Programming Languages Functional Programming Languages: More Haskell Nested definitions Lists Lecture notes, exercises, etc., can.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
5/10/20151 GC16/3011 Functional Programming Lecture 13 Example Programs 1. Evaluating arithmetic expressions 2. lists as functions.
Com Functional Programming Algebraic Data Types Marian Gheorghe Lecture 12 Module homepage Mole & ©University of.
Comp 205: Comparative Programming Languages Functional Programming Languages: Haskell Lecture notes, exercises, etc., can be found at:
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.
Defining new types of data. Defining New Datatypes Ability to add new datatypes in a programming language is important. Kinds of datatypes – enumerated.
Movement. Fixed Movement setLocation (x, y) Makes the crab move to a fixed cell x,y Relative Movement The functions: getX() getY() Fetch the x and y coordinate.
5. Types and Polymorphism. © O. Nierstrasz PS — Type Systems 5.2 Roadmap  Static and Dynamic Types  Type Completeness  Types in Haskell  Monomorphic.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Comp 205: Comparative Programming Languages Imperative Programming Languages Functional Programming Languages Semantics Other Paradigms Lecture notes,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 19: Functions, Types and Data Structures in Haskell COMP 144 Programming Language.
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.
Data Types.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Review of C++ Programming Part II Sheng-Fang Huang.
Functional Programming Element of Functional Programming.
Operators, Functions and Modules1 Pattern Matching & Recursion.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
Chapter 9: Functional Programming in a Typed Language.
Java Programming: Program Design Including Data Structures 1 Vectors The class Vector can be used to implement a list Unlike an array, the size of a Vector.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
11/1/20151 GC16/3011 Functional Programming Lecture 5 Miranda patterns, functions, recursion and lists.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Overview of the Haskell 98 Programming Language
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
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,
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
An introduction to functional programming using Haskell CENG242 –Recitation 1.
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)
Java Programming Language Lecture27- An Introduction.
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.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
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 IN HASKELL
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Haskell Chapter 2.
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
Comp 205: Comparative Programming Languages
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Types and Classes in Haskell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Functions and patterns
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

Comp 205: Comparative Programming Languages User-Defined Types Enumerated types Parameterised types Recursive types Lecture notes, exercises, etc., can be found at:

Introducing Constants Often it is useful to declare constants in programs: to make the program easier to read, and/or to group related definitions together In Java, we might declare: public static final NORTH = 0; public static final EAST = 1; public static final SOUTH = 2; public static final WEST = 3;

Turning 90º Because the constants are just names for integers, code can be hard to follow: public int turn(int direction) { direction++; if (direction < 0) || (3 < direction) return NORTH; return direction; }

Turning 90º Again Not referring to the representation ( int ), makes the code easier to follow: public int turn(int direction) { switch (direction) { case NORTH: return EAST; case EAST : return SOUTH; case SOUTH: return WEST; default : return NORTH; }

Putting on the Style Haskell allows the programmer to define types: data CardinalPoint = North | East | South | West The interpreter treats these as new values of a new type: Main> :t East East :: CardinalPoint Main> East ERROR - cannot find "show" function...

Deriving Classes data CardinalPoint = North | East | South | West deriving (Eq, Show) Main> East East Main> East == West False

Constructor Review "[]" and ":" are constructors for lists: they are primitive operators (i.e., not evaluated) all lists are built from these operators (and elements of the "parameter type")

Some Constructors Bool : True and False [a] : [] and : (a,b) : (_,_) Char : 'a', 'b',...

Enumerated Types Types defined to have only constants are referred to as enumerated types: data Colours = Red | Orange | Yellow | Green | Blue | Purple -- Bool is a built-in enumerated type: data Bool = True | False

True and False The type Bool is a built-in enumerated type, with constructors True and False ; these can be used in pattern-matching: not :: Bool -> Bool not True = False not False = True

Constant Constructors The constants in enumerated types are constructors, and can be used in pattern-matching: turn :: CardinalPoint -> CardinalPoint turn North = East turn East = South turn South = West turn West = North Main> turn East South

Constructors with Args Often we need types where the constructors take arguments: data Point = Pt (Int,Int) deriving (Show) Main> Pt (3,6) Pt (3,6) Main> :t Pt (3,6) Pt (3,6) :: Point main> :t Pt Pt :: (Int,Int) -> Point

Constructors with Args Constructors can be used in pattern-matching: data Point = Pt (Int,Int) deriving (Show) getX :: Point -> Int getX (Pt (x,y)) = x Main> getX (Pt (3,6)) 3

Constructor Curry We can use currying for constructors that take more than one argument: data IntString = IS Int [Char] getString :: IntString -> [Char] getString (IS n s) = s Main> getString (IS 7 "prosper") prosper

Type Parameters Types can be defined over arbitrary types using type variables: data ThingAtPoint a = At (a,Point) deriving Show Main> :t At(0::Int, Pt(2,4))... :: ThingAtPoint Int Main> :t At("begin", Pt(2,4))... :: ThingAtPoint [Char]

More Type Parameters Types that have more than one type parameter use more than one type variable: data ThingAndList a b = TL a [b] deriving Show getList :: ThingAndList a b -> [b] getList (TL t l) = l Main> getList (TL 7 "prosper") prosper

Polymorphism Parameterised definitions allow polymorphic functions: data IntList a = IL Int [a] deriving Show mapList :: (a -> b) -> IntList a -> IntList b mapList f (IL n l) = IL n (map f l) Main> mapList ord (IL 7 "abc") NL 7 [97,98,99]

Summary Users (programmers) can define types by specifying constructors and their types these types are distinct from all other types (and constructor names should not be reused) definitions can be parameterised over types Next: more user-defined types (trees and catamorphisms)