Programming in Haskell Aug-Nov 2015

Similar documents
CSCE 314 Programming Languages

Programming Languages Fall 2013

Lecture 19: Functions, Types and Data Structures in Haskell

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Overloading, Type Classes, and Algebraic Datatypes

Functional Programming in Haskell Part I : Basics

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

12 CREATING NEW TYPES

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

G Programming Languages - Fall 2012

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CSE 3302 Programming Languages Lecture 8: Functional Programming

Haskell Making Our Own Types and Typeclasses

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Stacks and Queues

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Industrial Programming

CSC324 Principles of Programming Languages

CSCE 314 Programming Languages

Programming in Haskell Aug-Nov 2015

Programming in Haskell Aug-Nov 2015

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

COP4020 Programming Assignment 1 - Spring 2011

02157 Functional Programming Tagged values and Higher-order list functions

A general introduction to Functional Programming using Haskell

Introduction to Programming: Lecture 10

Haskell Types COMP360

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Haskell An Introduction

Introduction to Haskell

Advanced features of Functional Programming (Haskell)

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Programming in Haskell Aug Nov 2015

CS3110 Spring 2016 Lecture 6: Modules

An introduction introduction to functional functional programming programming using usin Haskell

Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

Topic 7: Algebraic Data Types

Algebraic Types. Chapter 14 of Thompson

Lecture 12: Data Types (and Some Leftover ML)

An introduction to functional programming. July 23, 2010

Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Object-Oriented Principles and Practice / C++

Background Type Classes (1B) Young Won Lim 6/14/18

Haskell Overview II (2A) Young Won Lim 8/9/16

02157 Functional Programming. Michael R. Ha. Disjoint Unions and Higher-order list functions. Michael R. Hansen

Introduction to Programming, Aug-Dec 2006

Type-indexed functions in Generic Haskell

Introduction to Programming: Lecture 3

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Haskell Overview III (3A) Young Won Lim 10/4/16

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Stack and Queue. Stack:

Linked Structures. See Section 3.2 of the text.

Stacks and queues (chapters 6.6, 15.1, 15.5)

CSC324 Principles of Programming Languages

Structural polymorphism in Generic Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 360: Programming Languages Lecture 12: More Haskell

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Background Type Classes (1B) Young Won Lim 6/28/18

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Tree Equality: The Problem

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CS 457/557: Functional Languages

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

Programming Paradigms

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Parallel Haskell on MultiCores and Clusters

Functional Programming. Lecture 2: Algebra

CS 440: Programming Languages and Translators, Spring 2019 Mon

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

Haskell Revision and Exercises

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

Inductive Data Types

CS 320: Concepts of Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Logic - CM0845 Introduction to Haskell

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

ITI Introduction to Computing II

Francesco Nidito. Programmazione Avanzata AA 2007/08

Transcription:

Programming in Haskell Aug-Nov 2015 LECTURE 14 OCTOBER 1, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE

Enumerated data types The data keyword is used to define new types data Bool = False True data Day = Sun Mon Tue Wed Thu Fri Sat

Data types with parameters data Shape = Circle Float Square Float Rectangle Float Float Circle 5.0, Square 4.0, Rectangle 3.0 4.0

Functions on data types Functions can be defined using pattern matching weekend :: Day -> Bool weekend Sat = True weekend Sun = True weekend _ = False area :: Shape -> Float area (Circle r) = pi*r*r area (Square x) = x*x area (Rectangle l w) = l*w where pi = 3.1415927

Functions on data types What about weekend2 :: Day -> Bool weekend2 d (d == Sat d == Sun) = True otherwise = False Error - No instance for (Eq Day) arising from a use of ==

Functions on data types How about this function? nextday :: Day -> Day nextday Sun = Mon nextday Mon = Tue... nextday Sat = Sun Invoking nextday Fri in ghci will lead to error Error - No instance for (Show Day) arising from a use of 'print'

Add data types to typeclasses To check equality of two values of a data type a, a must belong the type class Eq We add Day to the type class Eq as follows data Day = Sun Mon... Sat deriving Eq Default behaviour: Sun == Sun, Tue /= Fri,... Now weekday2 compiles without error

The type class Show To make nextday work, we must make Day an instance of Show data Day = Sun Mon... Sat deriving (Eq, Show) The type class Show consists of all data types that implement the function show

More derivations show converts its input to a string which can be printed on the screen Default text representation show Wed == "Wed" data Day = Sun Mon... Sat deriving (Eq, Show, Ord) Sun < Mon <... < Sat

More derivations... data Shape = Circle Float Square Float Rectangle Float Float deriving (Eq, Ord, Show) show (Circle 5.0) == "Circle 5.0" Square 4.0 == Square 4.0 Square 4.0 /= Square 3.0 Circle 5.0 /= Rectangle 3.0 4.0 Square 4.0 > Circle 5.0

Constructors Square, Circle, Sun, Mon,... are constructors They are functions Sun :: Day Rectangle :: Float -> Float -> Shape Circle :: Float -> Shape

Constructors... Constructors can be used just like other functions Circle 5.0 :: Shape map Circle :: [Float] -> [Shape] map Circle [3.0, 2.0] = [Circle 3.0, Circle 2.0]

Records data Person = Person String Int Float String deriving Show guy = Person "Alpha" 21 5.8 "+914427470226" name :: Person -> String name (Person n _) = n age :: Person -> Int age (Person _ a ) = a

Records... height :: Person -> Float height (Person h _) = h phone :: Person -> Int phone (Person _ p) = p

Record syntax data Person = Person { name :: String, age :: Int, height :: Float, phone :: String } deriving Show guy = Person {name="alpha", age = 21, height = 5.8, phone = "+914427470226"} The field names are actually functions name :: Person -> String age :: Person -> Int

Summary The keyword data is used to declare new data types The keyword deriving to derive as an instance of a type class Data types with parameters - Shape, Person Sum type or union - Day, Shape Product type or struct - Person

Abstract data types Consider a Stack data type a collection of Ints stacked one on top of the other push: place an element on top of the stack pop: remove the topmost element of the stack Behaviour similar to lists

Abstract data types type Stack = [Int] push :: Int -> Stack -> Stack push x s = x:s pop :: Stack -> (Int, Stack) pop (x:s') = (x, s') Internal representation is evident. Stack is just a synonym insert :: Int -> Int -> Stack -> Stack insert x n s = (take (n-1) s) ++ [x] ++ (drop (n-1) s)

Abstract data types data Stack = Stack [Int] The value constructor Stack is a function that converts a list of Int to a Stack object Internal representation hidden

Abstract data types empty :: Stack empty = Stack [] push :: Int -> Stack -> Stack push x (Stack xs) = Stack (x:xs) pop :: Stack -> (Int, Stack) pop (Stack (x:xs)) = (x, Stack xs) isempty :: Stack -> Bool isempty (Stack []) = True isempty (Stack _) = False

Type parameters Polymorphic user-defined data types data Stack a = Stack [a] deriving (Eq, Show, Ord) empty :: Stack a push :: Int -> Stack a -> Stack a pop :: Stack a -> (a, Stack a) isempty :: Stack a -> Bool

Type parameters... Suppose we want to sum all elements in a stack sumstack (Stack xs) = sum xs What is the type of sumstack? Applicable only if the stack has numeric elements sumstack :: (Num a) => Stack a -> a

A custom show show (Stack [1,2,3]) == "Stack [1,2,3]" deriving Show defines a default implementation for show Suppose we want something mildly fancy show (Stack [1,2,3]) == "1->2->3"

A custom show One can change the default behaviour printelems :: (Show a) => [a] -> String printelems [] = "" printelems [x] = show x printelems (x:xs) = show x ++ "->" ++ printelems xs instance (Show a) => Show (Stack a) where show (Stack l) = printelems l

Queues Consider a Queue data type a collection of Ints arranged in a sequence enqueue: add an element at the end of the queue dequeue: remove the element at the start of the queue

Queues data Queue a = Queue [a] empty :: Queue a empty = Queue [] isempty :: Queue a -> Bool isempty (Queue []) = True isempty (Queue _) = False

Queues enqueue :: a -> Queue a -> Queue a enqueue x (Queue xs) = Queue (xs ++ [x]) dequeue :: Queue a -> (a, Queue a) dequeue (Queue (x:xs)) = (x, Queue xs)

Queues Each enque on a queue of length n takes O(n) time Enqueueing and dequeueing n elements might take O(n 2 ) time

Efficient queue Use two lists Represent q 1, q 2,..., q n as [q 1, q 2,..., q j ]and [q n, q n-1,..., q j+1 ] Second list is the second part of queue in reversed order enqueue adds an element at the start of the second list dequeue removes an element from the start of the first list

Efficient queue What if we try to dequeue when the first list is empty? We reverse the second list into the first, and remove the first element

Efficient queue data Queue a = NuQu [a] [a] enqueue x (NuQu ys zs) = NuQu ys (x:zs) dequeue (NuQu (x:xs) ys) = (x, NuQu xs ys) dequeue (NuQu [] ys) = dequeue (NuQu (reverse ys) [])

Efficient queue If we add n elements, we get a queue NuQu [] [qn,qn-1,...,q1] Next dequeue takes O(n) time to reverse the list After one dequeue we get NuQu [q2,...,qn] [] Next n-1 dequeue operations take O(1) time

Amortized analysis How many times is an element touched? Once when it is added to the second list Twice when it is moved from the second to first Once when it is removed from the first list Each element is touched at most four times Any sequence of n instructions involves at most n elements So any sequence of n instructions takes only O(n) steps

Summary Abstract data types We can define polymorphic user-defined data types by supplying type parameters Conditional polymorphism for functions defined on such data The instance keyword to define non-default implementation of functions Efficient queues and amortized analysis