Functional Programming in Haskell Part I : Basics

Similar documents
Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Programming Language Concepts: Lecture 14

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Introduction to Programming, Aug-Dec 2006

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Introduction to Programming: Lecture 6

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

Programming Languages Fall 2013

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

A general introduction to Functional Programming using Haskell

Introduction to Programming, Aug-Dec 2008

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

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

Shell CSCE 314 TAMU. Functions continued

An introduction introduction to functional functional programming programming using usin Haskell

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Programming in Haskell Aug-Nov 2015

Lecture 19: Functions, Types and Data Structures in Haskell

Haskell through HUGS THE BASICS

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

Programming in Haskell Aug-Nov 2015

Lecture 1 August 9, 2017

Overloading, Type Classes, and Algebraic Datatypes

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Shell CSCE 314 TAMU. Haskell Functions

Introduction. chapter Functions

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

Course year Typeclasses and their instances

An introduction to functional programming. July 23, 2010

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

CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

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

Advanced features of Functional Programming (Haskell)

CSE 3302 Programming Languages Lecture 8: Functional Programming

Programming in Haskell Aug-Nov 2015

LECTURE 16. Functional Programming

Introduction to Programming: Lecture 3

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

Mini-ML. CS 502 Lecture 2 8/28/08

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

Programming Paradigms

CS 320: Concepts of Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Recall our recursive multiply algorithm:

Haskell Types COMP360

CSc 372 Comparative Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Languages 3. Definition and Proof by Induction

Functional Programming and Haskell

CS 11 Haskell track: lecture 1

Advances in Programming Languages

SML A F unctional Functional Language Language Lecture 19

Extended Static Checking for Haskell (ESC/Haskell)

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

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

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

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Haskell An Introduction

Logical Methods in... using Haskell Getting Started

Lecture 2: List algorithms using recursion and list comprehensions

Functional Programming for Logicians - Lecture 1

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Programming Language Concepts: Lecture 1

Lists. Michael P. Fourman. February 2, 2010

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

CS 320: Concepts of Programming Languages

Functional Programming in Haskell Prof. Madhavan Mukund and S. P. Suresh Chennai Mathematical Institute

CSCE 314 Programming Languages

Programming Paradigms

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

CS 360: Programming Languages Lecture 12: More Haskell

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Tree Equality: The Problem

Shell CSCE 314 TAMU. Higher Order Functions

Functional Programming in Haskell for A level teachers

02157 Functional Programming Tagged values and Higher-order list functions

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Haskell-Tutorial. Damir Medak Gerhard Navratil. Institute for Geoinformation Technical University Vienna. February 2003

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

List Functions, and Higher-Order Functions

CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CS 457/557: Functional Languages

Standard prelude. Appendix A. A.1 Classes

CSc 520. Principles of Programming Languages 11: Haskell Basics

Chapter 15. Functional Programming Languages

Lecture 1 Functional Programming

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Data Types The ML Type System

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

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

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Transcription:

Functional Programming in Haskell Part I : Basics Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai 600 017, India madhavan@cmi.ac.in http://www.cmi.ac.in/ madhavan Madras Christian College, 8 December 2003 p.1

Functions Transform inputs to output Operate on specific types x n Real Integer power Real x These functions are different Real Real plus Real Integer Integer plus Integer Madras Christian College, 8 December 2003 p.2

Functional programming Program set of function definitions Function definition how to calculate the value Declarative programming Provably correct programs Functional program closely follows mathematical definition Rapid prototyping Easy to go from specification (what we require) to implementation (working program) Madras Christian College, 8 December 2003 p.3

Functional programming in Haskell Built-in types Int, Float, Bool,... with basic operations +, -, *, /,, &&,... Madras Christian College, 8 December 2003 p.4

Functional programming in Haskell Built-in types Int, Float, Bool,... with basic operations +, -, *, /,, &&,... Defining a new function (and its type) power :: Float -> Int -> Float power x n = if (n == 0) then 1.0 else x * (power x (n-1)) Madras Christian College, 8 December 2003 p.4

Functional programming in Haskell Built-in types Int, Float, Bool,... with basic operations +, -, *, /,, &&,... Defining a new function (and its type) power :: Float -> Int -> Float power x n = if (n == 0) then 1.0 else x * (power x (n-1)) Multiple arguments are consumed one at a time Not... power :: Float Int -> Float power (x,n) =... Madras Christian College, 8 December 2003 p.4

Functional programming in Haskell Built-in types Int, Float, Bool,... with basic operations +, -, *, /,, &&,... Defining a new function (and its type) power :: Float -> Int -> Float power x n = if (n == 0) then 1.0 else x * (power x (n-1)) Multiple arguments are consumed one at a time Not... power :: Float Int -> Float power (x,n) =... Need not be a total function What is power 2.0-1? Madras Christian College, 8 December 2003 p.4

Ways of defining functions Multiple definitions, read top to bottom Madras Christian College, 8 December 2003 p.5

Ways of defining functions Multiple definitions, read top to bottom Definition by cases power :: Float -> Int -> Float power x 0 = 1.0 power x n = x * (power x (n-1)) Madras Christian College, 8 December 2003 p.5

Ways of defining functions Multiple definitions, read top to bottom Definition by cases power :: Float -> Int -> Float power x 0 = 1.0 power x n = x * (power x (n-1)) Implicit pattern matching of arguments xor :: Bool -> Bool -> Bool xor True True = False xor False False = False xor x y = True Madras Christian College, 8 December 2003 p.5

Ways of defining functions... Multiple options with conditional guards max :: Int -> Int -> Int max i j (i >= j) = i (i < j) = j Madras Christian College, 8 December 2003 p.6

Ways of defining functions... Multiple options with conditional guards max :: Int -> Int -> Int max i j (i >= j) = i (i < j) = j Default conditional value otherwise max3 :: Int -> Int -> Int -> Int max3 i j k (i >= j) && (i >= k) = i (j >= k) = j otherwise = k Madras Christian College, 8 December 2003 p.6

Ways of defining functions... Multiple options with conditional guards max :: Int -> Int -> Int max i j (i >= j) = i (i < j) = j Default conditional value otherwise max3 :: Int -> Int -> Int -> Int max3 i j k (i >= j) && (i >= k) = i (j >= k) = j otherwise = k Note: Conditional guards are evaluated top to bottom! Madras Christian College, 8 December 2003 p.6

Pairs, triples,... Can form n-tuples of types Madras Christian College, 8 December 2003 p.7

Pairs, triples,... Can form n-tuples of types (x,y,z) :: (Float,Float,Float) represents a point in 3D Madras Christian College, 8 December 2003 p.7

Pairs, triples,... Can form n-tuples of types (x,y,z) :: (Float,Float,Float) represents a point in 3D Can define a function distance3d :: (Float,Float,Float) -> (Float,Float,Float) -> Float Madras Christian College, 8 December 2003 p.7

Pairs, triples,... Can form n-tuples of types (x,y,z) :: (Float,Float,Float) represents a point in 3D Can define a function distance3d :: (Float,Float,Float) -> (Float,Float,Float) -> Float Functions can return n-tuples maxandminof3 :: Int -> Int -> Int -> (Int,Int) Madras Christian College, 8 December 2003 p.7

Local definitions using where Example: Compute distance between two points in 2D distance :: (Float,Float)->(Float,Float)->Float distance (x1,y1) (x2,y2) = sqrt((sqr xdistance) + (sqr ydistance)) where xdistance = x2 - x1 ydistance = y2 - y1 sqr :: Float -> Float sqr z = z*z Madras Christian College, 8 December 2003 p.8

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) 3.0 * (power 3.0 1) Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) 3.0 * (power 3.0 1) 3.0 * 3.0 * (power 3.0 (1-1)) Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) 3.0 * (power 3.0 1) 3.0 * 3.0 * (power 3.0 (1-1)) 3.0 * 3.0 * (power 3.0 0) Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) 3.0 * (power 3.0 1) 3.0 * 3.0 * (power 3.0 (1-1)) 3.0 * 3.0 * (power 3.0 0) 3.0 * 3.0 * 1.0 Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) 3.0 * (power 3.0 1) 3.0 * 3.0 * (power 3.0 (1-1)) 3.0 * 3.0 * (power 3.0 0) 3.0 * 3.0 * 1.0 9.0 * 1.0 Madras Christian College, 8 December 2003 p.9

Computation is rewriting Use definitions to simplify expressions till no further simplification is possible Builtin simplifications 3 + 5 8 True False True Simplifications based on user defined functions power 3.0 2 3.0 * (power 3.0 (2-1)) 3.0 * (power 3.0 1) 3.0 * 3.0 * (power 3.0 (1-1)) 3.0 * 3.0 * (power 3.0 0) 3.0 * 3.0 * 1.0 9.0 * 1.0 9.0 Madras Christian College, 8 December 2003 p.9

Functions that manipulate functions A function with input type a and output type b has type a -> b Madras Christian College, 8 December 2003 p.10

Functions that manipulate functions A function with input type a and output type b has type a -> b Recall useful convention that all functions read one argument at a time! Madras Christian College, 8 December 2003 p.10

Functions that manipulate functions A function with input type a and output type b has type a -> b Recall useful convention that all functions read one argument at a time! A function can take another function as argument Madras Christian College, 8 December 2003 p.10

Functions that manipulate functions A function with input type a and output type b has type a -> b Recall useful convention that all functions read one argument at a time! A function can take another function as argument apply :: (Int -> Int) -> Int -> Int apply f n = f n Madras Christian College, 8 December 2003 p.10

Functions that manipulate functions A function with input type a and output type b has type a -> b Recall useful convention that all functions read one argument at a time! A function can take another function as argument apply :: (Int -> Int) -> Int -> Int apply f n = f n twice :: (Int -> Int) -> Int -> Int twice f n = f (f n) Madras Christian College, 8 December 2003 p.10

Functions that manipulate functions A function with input type a and output type b has type a -> b Recall useful convention that all functions read one argument at a time! A function can take another function as argument apply :: (Int -> Int) -> Int -> Int apply f n = f n twice :: (Int -> Int) -> Int -> Int twice f n = f (f n) twice sqr 7 sqr (sqr 7) sqr (7*7) 49*49 2401 Madras Christian College, 8 December 2003 p.10

Running Haskell programs hugs A Haskell interpreter Available for Linux, Windows,... ghc the Glasgow Haskell Compiler Look at http://www.haskell.org Madras Christian College, 8 December 2003 p.11

Collections Basic collective type is a list Madras Christian College, 8 December 2003 p.12

Collections Basic collective type is a list All items of a list must be of the same type [Int] list of Int, [(Float,Float)] list of pairs of Float Madras Christian College, 8 December 2003 p.12

Collections Basic collective type is a list All items of a list must be of the same type [Int] list of Int, [(Float,Float)] list of pairs of Float Lists are written as follows: [2,3,1,7] [(3.0,7.5),(7.6,9.2),(3.3,7.868)] Madras Christian College, 8 December 2003 p.12

Collections Basic collective type is a list All items of a list must be of the same type [Int] list of Int, [(Float,Float)] list of pairs of Float Lists are written as follows: [2,3,1,7] [(3.0,7.5),(7.6,9.2),(3.3,7.868)] Empty list is denoted [] (for all types) Madras Christian College, 8 December 2003 p.12

Basic operations on lists ++ concatenates lists [1,3] ++ [5,7] = [1,3,5,7] Madras Christian College, 8 December 2003 p.13

Basic operations on lists ++ concatenates lists [1,3] ++ [5,7] = [1,3,5,7] Unique way of decomposing a nonempty list head : first element of the list tail : the rest (may be empty!) head [1,3,5,7] = 1 tail [1,3,5,7] = [3,5,7] Madras Christian College, 8 December 2003 p.13

Basic operations on lists ++ concatenates lists [1,3] ++ [5,7] = [1,3,5,7] Unique way of decomposing a nonempty list head : first element of the list tail : the rest (may be empty!) head [1,3,5,7] = 1 tail [1,3,5,7] = [3,5,7] Note the types: head is an element, tail is a list Madras Christian College, 8 December 2003 p.13

Basic operations on lists ++ concatenates lists [1,3] ++ [5,7] = [1,3,5,7] Unique way of decomposing a nonempty list head : first element of the list tail : the rest (may be empty!) head [1,3,5,7] = 1 tail [1,3,5,7] = [3,5,7] Note the types: head is an element, tail is a list Write x:l to denote the list with head x, tail l Madras Christian College, 8 December 2003 p.13

Functions on lists Define functions by induction on list structure Madras Christian College, 8 December 2003 p.14

Functions on lists Define functions by induction on list structure Base case Value of f on [] Step Extend value of f on l to f on x:l Madras Christian College, 8 December 2003 p.14

Functions on lists Define functions by induction on list structure Base case Value of f on [] Step Extend value of f on l to f on x:l length :: [Int] -> Int length [] = 0 length (x:l) = 1 + length l Madras Christian College, 8 December 2003 p.14

Functions on lists Define functions by induction on list structure Base case Value of f on [] Step Extend value of f on l to f on x:l length :: [Int] -> Int length [] = 0 length (x:l) = 1 + length l reverse :: [Int] -> [Int] reverse [] = [] reverse (x:l) = (reverse l) ++ [x] Madras Christian College, 8 December 2003 p.14

Some builtin list functions length l, reverse l, sum l,... Madras Christian College, 8 December 2003 p.15

Some builtin list functions length l, reverse l, sum l,... head l, tail l Madras Christian College, 8 December 2003 p.15

Some builtin list functions length l, reverse l, sum l,... head l, tail l Dually, init l, last l init [1,2,3] = [1,2], last [1,2,3] = 3 Madras Christian College, 8 December 2003 p.15

Some builtin list functions length l, reverse l, sum l,... head l, tail l Dually, init l, last l init [1,2,3] = [1,2], last [1,2,3] = 3 take n l extract the first n elements of l drop n l drop the first n elements of l Madras Christian College, 8 December 2003 p.15

Some builtin list functions length l, reverse l, sum l,... head l, tail l Dually, init l, last l init [1,2,3] = [1,2], last [1,2,3] = 3 take n l extract the first n elements of l drop n l drop the first n elements of l Shortcut list notation Madras Christian College, 8 December 2003 p.15

Some builtin list functions length l, reverse l, sum l,... head l, tail l Dually, init l, last l init [1,2,3] = [1,2], last [1,2,3] = 3 take n l extract the first n elements of l drop n l drop the first n elements of l Shortcut list notation [m..n] abbreviates the list [m,m+1,...,n] Example [3..7] = [3,4,5,6,7] Madras Christian College, 8 December 2003 p.15

Some builtin list functions length l, reverse l, sum l,... head l, tail l Dually, init l, last l init [1,2,3] = [1,2], last [1,2,3] = 3 take n l extract the first n elements of l drop n l drop the first n elements of l Shortcut list notation [m..n] abbreviates the list [m,m+1,...,n] Example [3..7] = [3,4,5,6,7] Arithmetic progressions [1,3..8] = [1,3,5,7] [9,8..5] = [9,8,7,6,5] Madras Christian College, 8 December 2003 p.15

Operating on each element of a list map f l applies f to each item of l Madras Christian College, 8 December 2003 p.16

Operating on each element of a list map f l applies f to each item of l square :: Int -> Int square n = n*n map square [1,2,4,9] [2,4,9,81] Madras Christian College, 8 December 2003 p.16

Operating on each element of a list map f l applies f to each item of l square :: Int -> Int square n = n*n map square [1,2,4,9] [2,4,9,81] filter p l selects items from l that satisfy p Madras Christian College, 8 December 2003 p.16

Operating on each element of a list map f l applies f to each item of l square :: Int -> Int square n = n*n map square [1,2,4,9] [2,4,9,81] filter p l selects items from l that satisfy p even :: Int -> Bool even x = (mod x 2 == 0) filter even [1,2,4,9] [2,4] Madras Christian College, 8 December 2003 p.16

Operating on each element of a list map f l applies f to each item of l square :: Int -> Int square n = n*n map square [1,2,4,9] [2,4,9,81] filter p l selects items from l that satisfy p even :: Int -> Bool even x = (mod x 2 == 0) filter even [1,2,4,9] [2,4] Can compose these functions Madras Christian College, 8 December 2003 p.16

Operating on each element of a list map f l applies f to each item of l square :: Int -> Int square n = n*n map square [1,2,4,9] [2,4,9,81] filter p l selects items from l that satisfy p even :: Int -> Bool even x = (mod x 2 == 0) filter even [1,2,4,9] [2,4] Can compose these functions map square (filter even [1..10]) [4,16,36,64,100] Madras Christian College, 8 December 2003 p.16

List comprehension: New lists from old The set of squares of the even numbers between 1 and 10 {x 2 x {1,..., 10}, even(x)} Madras Christian College, 8 December 2003 p.17

List comprehension: New lists from old The set of squares of the even numbers between 1 and 10 {x 2 x {1,..., 10}, even(x)} The list of squares of the even numbers between 1 and 10 [ square x x <- [1..10], even x ] where even x = (mod x 2 == 0) square x = x*x Madras Christian College, 8 December 2003 p.17

Using list comprehensions... divisors :: Int -> [Int] divisors n = [ m m <- [1..n], mod n m == 0 ] Madras Christian College, 8 December 2003 p.18

Using list comprehensions... divisors :: Int -> [Int] divisors n = [ m m <- [1..n], mod n m == 0 ] prime :: Int -> Bool prime n = (divisors n == [1,n]) Madras Christian College, 8 December 2003 p.18

Example: Quicksort Choose an element of the list as a splitter and create sublists of elements smaller than the splitter and larger than the splitter Recursively sort these sublists and combine Madras Christian College, 8 December 2003 p.19

Example: Quicksort Choose an element of the list as a splitter and create sublists of elements smaller than the splitter and larger than the splitter Recursively sort these sublists and combine qsort [] = [] qsort l = qsort lower ++ [splitter] ++ qsort upper where splitter = head l lower = [i i <- tail l, i <= splitter] upper = [i i <- tail l, i > splitter] Madras Christian College, 8 December 2003 p.19

Polymorphism Are length :: [Int] -> Int length :: [Float] -> Int different functions? Madras Christian College, 8 December 2003 p.20

Polymorphism Are length :: [Int] -> Int length :: [Float] -> Int different functions? length only looks at the structure of the list, not into individual elements For any underlying type t, length :: [t] -> Int Madras Christian College, 8 December 2003 p.20

Polymorphism Are length :: [Int] -> Int length :: [Float] -> Int different functions? length only looks at the structure of the list, not into individual elements For any underlying type t, length :: Use a,b,... to denote generic types So, length :: [a] -> Int [t] -> Int Madras Christian College, 8 December 2003 p.20

Polymorphism Are length :: [Int] -> Int length :: [Float] -> Int different functions? length only looks at the structure of the list, not into individual elements For any underlying type t, length :: Use a,b,... to denote generic types So, length :: [a] -> Int Similarly, the most general type of reverse is reverse :: [a] -> [a] [t] -> Int Madras Christian College, 8 December 2003 p.20

Polymorphism versus overloading True polymorphism The same computation is performed for different types Madras Christian College, 8 December 2003 p.21

Polymorphism versus overloading True polymorphism The same computation is performed for different types Overloading Same symbol or function name denotes different computations for different types Madras Christian College, 8 December 2003 p.21

Polymorphism versus overloading True polymorphism The same computation is performed for different types Overloading Same symbol or function name denotes different computations for different types Example Arithmetic operators At bit level, algorithms for Int + Int and Float + Float are different Madras Christian College, 8 December 2003 p.21

Polymorphism versus overloading True polymorphism The same computation is performed for different types Overloading Same symbol or function name denotes different computations for different types Example Arithmetic operators At bit level, algorithms for Int + Int and Float + Float are different What about subclass polymorphism in OO programming? Madras Christian College, 8 December 2003 p.21

Polymorphism versus overloading in OO class Shape { } class Circle extends Shape { double size {return pi*radius*radius} } class Square extends Shape { double size {return side*side} } Shape s1 = new Circle; print s1.size(); Shape s2 = new Square; print s2.size(); Madras Christian College, 8 December 2003 p.22

Polymorphism versus overloading in OO class Shape { } class Circle extends Shape { double size {return pi*radius*radius} } class Square extends Shape { double size {return side*side} } Shape s1 = new Circle; print s1.size(); Shape s2 = new Square; print s2.size(); Implementation of size is different!! Madras Christian College, 8 December 2003 p.22

Conditional polymorphism What about member x [] = False member x (y:l) (x == y) = True otherwise = member x l Madras Christian College, 8 December 2003 p.23

Conditional polymorphism What about member x [] = False member x (y:l) (x == y) = True otherwise = member x l Is member :: a -> [a] -> Bool a valid description of the type? Madras Christian College, 8 December 2003 p.23

Conditional polymorphism What about member x [] = False member x (y:l) (x == y) = True otherwise = member x l Is member :: a -> [a] -> Bool a valid description of the type? What is the value of member qsort [qsort, mergesort, plus]? Equality of functions cannot be checked effectively Madras Christian College, 8 December 2003 p.23

Conditional polymorphism What about member x [] = False member x (y:l) (x == y) = True otherwise = member x l Is member :: a -> [a] -> Bool a valid description of the type? What is the value of member qsort [qsort, mergesort, plus]? Equality of functions cannot be checked effectively The underlying type should support equality Madras Christian College, 8 December 2003 p.23

Type classes Haskell organizes types into classes. A type class is a subset of all types. The class Eq contains all types that support == on their elements. The predicate Eq a tells whether or not a belongs to Eq Haskell would type this as member :: Eq a => a -> [a] -> Bool Likewise Ord a is the set of types that support comparison, so quicksort: Ord a => [a] -> [a] Madras Christian College, 8 December 2003 p.24

Examples of declarative programming Compute all initial segments of a list Madras Christian College, 8 December 2003 p.25

Examples of declarative programming Compute all initial segments of a list Initial segments of [] are empty Madras Christian College, 8 December 2003 p.25

Examples of declarative programming Compute all initial segments of a list Initial segments of [] are empty Initial segments of x:l all initial segments of l with x in front, plus the empty segment Madras Christian College, 8 December 2003 p.25

Examples of declarative programming Compute all initial segments of a list Initial segments of [] are empty Initial segments of x:l all initial segments of l with x in front, plus the empty segment initial :: [a] -> [[a]] initial [] = [[]] initial (x:l) = [[]] ++ [ x:z z <- initial l] Madras Christian College, 8 December 2003 p.25

Examples of declarative programming... The empty list [] has no permutations Madras Christian College, 8 December 2003 p.26

Examples of declarative programming... The empty list [] has no permutations Permutations of x:l Interleave x through each permutation of l Madras Christian College, 8 December 2003 p.26

Examples of declarative programming... The empty list [] has no permutations Permutations of x:l Interleave x through each permutation of l interleave :: a -> [a] -> [[a]] interleave x [] = [[x]] interleave x (y:l) = [x:y:l] ++ [y:l2 l2 <- (interleave x l)] perms :: [a] -> [[a]] perms [] = [[]] perms (x:l) = [z y <- perms l, z <- interleave x y] Madras Christian College, 8 December 2003 p.26

Second lecture User defined datatypes Stacks, queues, trees,... Hiding implementation details using modules Infinite data structures Madras Christian College, 8 December 2003 p.27