Functional Programming in Haskell Part 2 : Abstract dataypes and ...
Transcript of Functional Programming in Haskell Part 2 : Abstract dataypes and ...
![Page 1: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/1.jpg)
Functional Programming in Haskell
Part 2 : Abstract dataypes and “infinite” structures
Madhavan Mukund
Chennai Mathematical Institute
92 G N Chetty Rd, Chennai 600 017, India
http://www.cmi.ac.in/˜madhavan
Madras Christian College, 10 December 2003 – p.1
![Page 2: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/2.jpg)
Haskell review
� Program ≡ Collection of function definitions
� Computation ≡ Rewriting using definitions� Functions are associated with input and output types� isDigit :: Char -> Bool
isDigit ’0’ = TrueisDigit ’1’ = True...isDigit ’9’ = TrueisDigit c = False
� isDigit c| (c >= ’0’ && c <= ’9’) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.2
![Page 3: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/3.jpg)
Haskell review
� Program ≡ Collection of function definitions� Computation ≡ Rewriting using definitions
� Functions are associated with input and output types� isDigit :: Char -> Bool
isDigit ’0’ = TrueisDigit ’1’ = True...isDigit ’9’ = TrueisDigit c = False
� isDigit c| (c >= ’0’ && c <= ’9’) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.2
![Page 4: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/4.jpg)
Haskell review
� Program ≡ Collection of function definitions� Computation ≡ Rewriting using definitions� Functions are associated with input and output types
� isDigit :: Char -> Bool
isDigit ’0’ = TrueisDigit ’1’ = True...isDigit ’9’ = TrueisDigit c = False
� isDigit c| (c >= ’0’ && c <= ’9’) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.2
![Page 5: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/5.jpg)
Haskell review
� Program ≡ Collection of function definitions� Computation ≡ Rewriting using definitions� Functions are associated with input and output types� isDigit :: Char -> Bool
isDigit ’0’ = TrueisDigit ’1’ = True...isDigit ’9’ = TrueisDigit c = False
� isDigit c| (c >= ’0’ && c <= ’9’) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.2
![Page 6: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/6.jpg)
Haskell review
� Program ≡ Collection of function definitions� Computation ≡ Rewriting using definitions� Functions are associated with input and output types� isDigit :: Char -> Bool
isDigit ’0’ = TrueisDigit ’1’ = True...isDigit ’9’ = TrueisDigit c = False
� isDigit c| (c >= ’0’ && c <= ’9’) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.2
![Page 7: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/7.jpg)
Haskell review
� Program ≡ Collection of function definitions� Computation ≡ Rewriting using definitions� Functions are associated with input and output types� isDigit :: Char -> Bool
isDigit ’0’ = TrueisDigit ’1’ = True...isDigit ’9’ = TrueisDigit c = False
� isDigit c| (c >= ’0’ && c <= ’9’) = True
| otherwise = FalseMadras Christian College, 10 December 2003 – p.2
![Page 8: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/8.jpg)
Haskell review . . .
� Basic collective type is a list
� Define list functions by induction on structure� Example Adding up a list of integers
sum :: [Int] -> Int
sum [] = 0
sum (x:l) = x + (sum l)
� (Conditional) polymorphism
Most general type of sum is
sum :: (Num a) => [a] -> a
where Num a is true for any type a that supports basicarithmetic operations +, -, . . .
Madras Christian College, 10 December 2003 – p.3
![Page 9: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/9.jpg)
Haskell review . . .
� Basic collective type is a list� Define list functions by induction on structure
� Example Adding up a list of integers
sum :: [Int] -> Int
sum [] = 0
sum (x:l) = x + (sum l)
� (Conditional) polymorphism
Most general type of sum is
sum :: (Num a) => [a] -> a
where Num a is true for any type a that supports basicarithmetic operations +, -, . . .
Madras Christian College, 10 December 2003 – p.3
![Page 10: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/10.jpg)
Haskell review . . .
� Basic collective type is a list� Define list functions by induction on structure� Example Adding up a list of integers
sum :: [Int] -> Int
sum [] = 0
sum (x:l) = x + (sum l)
� (Conditional) polymorphism
Most general type of sum is
sum :: (Num a) => [a] -> a
where Num a is true for any type a that supports basicarithmetic operations +, -, . . .
Madras Christian College, 10 December 2003 – p.3
![Page 11: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/11.jpg)
Haskell review . . .
� Basic collective type is a list� Define list functions by induction on structure� Example Adding up a list of integers
sum :: [Int] -> Int
sum [] = 0
sum (x:l) = x + (sum l)
� (Conditional) polymorphism
Most general type of sum is
sum :: (Num a) => [a] -> a
where Num a is true for any type a that supports basicarithmetic operations +, -, . . .
Madras Christian College, 10 December 2003 – p.3
![Page 12: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/12.jpg)
Haskell review . . .
� Basic collective type is a list� Define list functions by induction on structure� Example Adding up a list of integers
sum :: [Int] -> Int
sum [] = 0
sum (x:l) = x + (sum l)
� (Conditional) polymorphism
Most general type of sum is
sum :: (Num a) => [a] -> a
where Num a is true for any type a that supports basicarithmetic operations +, -, . . .
Madras Christian College, 10 December 2003 – p.3
![Page 13: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/13.jpg)
Haskell review . . .
� Basic collective type is a list� Define list functions by induction on structure� Example Adding up a list of integers
sum :: [Int] -> Int
sum [] = 0
sum (x:l) = x + (sum l)
� (Conditional) polymorphism
Most general type of sum is
sum :: (Num a) => [a] -> a
where Num a is true for any type a that supports basicarithmetic operations +, -, . . .
Madras Christian College, 10 December 2003 – p.3
![Page 14: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/14.jpg)
Today’s agenda
� Adding new types
� Defining abstract datatypes
Provide an interface that “hides” the implementation� Using “infinite” data structures
Madras Christian College, 10 December 2003 – p.4
![Page 15: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/15.jpg)
Today’s agenda
� Adding new types� Defining abstract datatypes
Provide an interface that “hides” the implementation
� Using “infinite” data structures
Madras Christian College, 10 December 2003 – p.4
![Page 16: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/16.jpg)
Today’s agenda
� Adding new types� Defining abstract datatypes
Provide an interface that “hides” the implementation� Using “infinite” data structures
Madras Christian College, 10 December 2003 – p.4
![Page 17: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/17.jpg)
User defined datatypes
� The data declaration adds new datatypes
� Enumerated types
data Signal = Red | Yellow | Green
� Can use this type in a function such as
stopwhen :: Signal -> Bool
stopwhen Red = True
stopwhen c = False
� What if we write instead
stopwhen2 :: Signal -> Boolstopwhen2 c | (c == Red) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.5
![Page 18: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/18.jpg)
User defined datatypes
� The data declaration adds new datatypes� Enumerated types
data Signal = Red | Yellow | Green
� Can use this type in a function such as
stopwhen :: Signal -> Bool
stopwhen Red = True
stopwhen c = False
� What if we write instead
stopwhen2 :: Signal -> Boolstopwhen2 c | (c == Red) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.5
![Page 19: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/19.jpg)
User defined datatypes
� The data declaration adds new datatypes� Enumerated types
data Signal = Red | Yellow | Green
� Can use this type in a function such as
stopwhen :: Signal -> Bool
stopwhen Red = True
stopwhen c = False
� What if we write instead
stopwhen2 :: Signal -> Boolstopwhen2 c | (c == Red) = True
| otherwise = False
Madras Christian College, 10 December 2003 – p.5
![Page 20: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/20.jpg)
User defined datatypes
� The data declaration adds new datatypes� Enumerated types
data Signal = Red | Yellow | Green
� Can use this type in a function such as
stopwhen :: Signal -> Bool
stopwhen Red = True
stopwhen c = False
� What if we write instead
stopwhen2 :: Signal -> Boolstopwhen2 c | (c == Red) = True
| otherwise = FalseMadras Christian College, 10 December 2003 – p.5
![Page 21: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/21.jpg)
User defined types and type classes
� stopwhen2 requires Eq Signal
� How aboutnextlight :: Signal -> Signal
nextlight Green = Yellow
nextlight Yellow = Red
nextlight Red = Green
� Displaying result of nextlight requiresShow Signal
� Show a is true of type a if there is a function
show :: a -> String
that allows values of a to be displayed
Madras Christian College, 10 December 2003 – p.6
![Page 22: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/22.jpg)
User defined types and type classes
� stopwhen2 requires Eq Signal
� How aboutnextlight :: Signal -> Signal
nextlight Green = Yellow
nextlight Yellow = Red
nextlight Red = Green
� Displaying result of nextlight requiresShow Signal
� Show a is true of type a if there is a function
show :: a -> String
that allows values of a to be displayed
Madras Christian College, 10 December 2003 – p.6
![Page 23: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/23.jpg)
User defined types and type classes
� stopwhen2 requires Eq Signal
� How aboutnextlight :: Signal -> Signal
nextlight Green = Yellow
nextlight Yellow = Red
nextlight Red = Green
� Displaying result of nextlight requiresShow Signal
� Show a is true of type a if there is a function
show :: a -> String
that allows values of a to be displayed
Madras Christian College, 10 December 2003 – p.6
![Page 24: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/24.jpg)
User defined types and type classes
� stopwhen2 requires Eq Signal
� How aboutnextlight :: Signal -> Signal
nextlight Green = Yellow
nextlight Yellow = Red
nextlight Red = Green
� Displaying result of nextlight requiresShow Signal
� Show a is true of type a if there is a function
show :: a -> String
that allows values of a to be displayed
Madras Christian College, 10 December 2003 – p.6
![Page 25: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/25.jpg)
Adding user defined types to type classes
� Simplest solution isdata Signal = Red | Yellow | Greenderiving (Eq, Show, Ord)
� Fixes default values
� Red == Red, Red /= Yellow, . . .� show Red = "Red",show Yellow = "Yellow", . . .
� Red < Yellow < Green
Madras Christian College, 10 December 2003 – p.7
![Page 26: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/26.jpg)
Adding user defined types to type classes
� Simplest solution isdata Signal = Red | Yellow | Greenderiving (Eq, Show, Ord)
� Fixes default values
� Red == Red, Red /= Yellow, . . .� show Red = "Red",show Yellow = "Yellow", . . .
� Red < Yellow < Green
Madras Christian College, 10 December 2003 – p.7
![Page 27: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/27.jpg)
Adding user defined types to type classes
� Simplest solution isdata Signal = Red | Yellow | Greenderiving (Eq, Show, Ord)
� Fixes default values
� Red == Red, Red /= Yellow, . . .
� show Red = "Red",show Yellow = "Yellow", . . .
� Red < Yellow < Green
Madras Christian College, 10 December 2003 – p.7
![Page 28: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/28.jpg)
Adding user defined types to type classes
� Simplest solution isdata Signal = Red | Yellow | Greenderiving (Eq, Show, Ord)
� Fixes default values
� Red == Red, Red /= Yellow, . . .� show Red = "Red",show Yellow = "Yellow", . . .
� Red < Yellow < Green
Madras Christian College, 10 December 2003 – p.7
![Page 29: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/29.jpg)
Adding user defined types to type classes
� Simplest solution isdata Signal = Red | Yellow | Greenderiving (Eq, Show, Ord)
� Fixes default values
� Red == Red, Red /= Yellow, . . .� show Red = "Red",show Yellow = "Yellow", . . .
� Red < Yellow < Green
Madras Christian College, 10 December 2003 – p.7
![Page 30: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/30.jpg)
Adding user defined types to type classes . . .
Or, provide your own functions
data Signal = Red | Yellow | Greenderiving (Eq)
instance Show Signal whereshow Yellow = "Yellow"show c = "Black"
instance Ord Signal whereGreen <= Yellow = TrueYellow <= Red = TrueRed <= Green = Truex <= y = False
In the class Ord, >, >=,. . . are defined in terms of <=Note: <= need not even be a consistent ordering!
Madras Christian College, 10 December 2003 – p.8
![Page 31: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/31.jpg)
Adding user defined types to type classes . . .
Or, provide your own functions
data Signal = Red | Yellow | Greenderiving (Eq)
instance Show Signal whereshow Yellow = "Yellow"show c = "Black"
instance Ord Signal whereGreen <= Yellow = TrueYellow <= Red = TrueRed <= Green = Truex <= y = False
In the class Ord, >, >=,. . . are defined in terms of <=Note: <= need not even be a consistent ordering!
Madras Christian College, 10 December 2003 – p.8
![Page 32: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/32.jpg)
Adding user defined types to type classes . . .
Or, provide your own functions
data Signal = Red | Yellow | Greenderiving (Eq)
instance Show Signal whereshow Yellow = "Yellow"show c = "Black"
instance Ord Signal whereGreen <= Yellow = TrueYellow <= Red = TrueRed <= Green = Truex <= y = False
In the class Ord, >, >=,. . . are defined in terms of <=Note: <= need not even be a consistent ordering!
Madras Christian College, 10 December 2003 – p.8
![Page 33: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/33.jpg)
Adding user defined types to type classes . . .
Or, provide your own functions
data Signal = Red | Yellow | Greenderiving (Eq)
instance Show Signal whereshow Yellow = "Yellow"show c = "Black"
instance Ord Signal whereGreen <= Yellow = TrueYellow <= Red = TrueRed <= Green = Truex <= y = False
In the class Ord, >, >=,. . . are defined in terms of <=Note: <= need not even be a consistent ordering!
Madras Christian College, 10 December 2003 – p.8
![Page 34: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/34.jpg)
Adding user defined types to type classes . . .
Or, provide your own functions
data Signal = Red | Yellow | Greenderiving (Eq)
instance Show Signal whereshow Yellow = "Yellow"show c = "Black"
instance Ord Signal whereGreen <= Yellow = TrueYellow <= Red = TrueRed <= Green = Truex <= y = False
In the class Ord, >, >=,. . . are defined in terms of <=
Note: <= need not even be a consistent ordering!
Madras Christian College, 10 December 2003 – p.8
![Page 35: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/35.jpg)
Adding user defined types to type classes . . .
Or, provide your own functions
data Signal = Red | Yellow | Greenderiving (Eq)
instance Show Signal whereshow Yellow = "Yellow"show c = "Black"
instance Ord Signal whereGreen <= Yellow = TrueYellow <= Red = TrueRed <= Green = Truex <= y = False
In the class Ord, >, >=,. . . are defined in terms of <=Note: <= need not even be a consistent ordering!
Madras Christian College, 10 December 2003 – p.8
![Page 36: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/36.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .� The constructor Node has three arguments
� First component Int: value stored at the node� Other two components Btreeint: left and right
subtrees
Madras Christian College, 10 December 2003 – p.9
![Page 37: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/37.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors
� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .� The constructor Node has three arguments
� First component Int: value stored at the node� Other two components Btreeint: left and right
subtrees
Madras Christian College, 10 December 2003 – p.9
![Page 38: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/38.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .� The constructor Node has three arguments
� First component Int: value stored at the node� Other two components Btreeint: left and right
subtrees
Madras Christian College, 10 December 2003 – p.9
![Page 39: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/39.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .
� The constructor Node has three arguments
� First component Int: value stored at the node� Other two components Btreeint: left and right
subtrees
Madras Christian College, 10 December 2003 – p.9
![Page 40: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/40.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .� The constructor Node has three arguments
� First component Int: value stored at the node� Other two components Btreeint: left and right
subtrees
Madras Christian College, 10 December 2003 – p.9
![Page 41: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/41.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .� The constructor Node has three arguments
� First component Int: value stored at the node
� Other two components Btreeint: left and rightsubtrees
Madras Christian College, 10 December 2003 – p.9
![Page 42: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/42.jpg)
Recursive datatypes
� A binary tree to store integers at each node
data Btreeint =Nil |
Node Int Btreeint Btreeint
� Nil and Node are constructors� The constructor Nil takes zero arguments
A constant, like Red, Green, . . .� The constructor Node has three arguments
� First component Int: value stored at the node� Other two components Btreeint: left and right
subtrees
Madras Christian College, 10 December 2003 – p.9
![Page 43: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/43.jpg)
Recursive datatypes . . .
Example
The tree6•
4 • 8•
7•
would be written as
Node 6 (Node 4 Nil Nil)
(Node 8 (Node 7 Nil Nil) Nil)
Madras Christian College, 10 December 2003 – p.10
![Page 44: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/44.jpg)
Functions on recursive datatypes
� Define by induction on the structure of datatype
� How many values are there in the tree?
size :: Btreeint -> Int
size Nil = 0
size (Node n t1 t2) = 1 + (size t1)
+ (size t2)
� List out all values in the tree
listout :: Btreeint -> [Int]
listout Nil = []
listout (Node n t1 t2) =
[n] ++ listout t1 ++ listout t2
Madras Christian College, 10 December 2003 – p.11
![Page 45: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/45.jpg)
Functions on recursive datatypes
� Define by induction on the structure of datatype� How many values are there in the tree?
size :: Btreeint -> Int
size Nil = 0
size (Node n t1 t2) = 1 + (size t1)
+ (size t2)
� List out all values in the tree
listout :: Btreeint -> [Int]
listout Nil = []
listout (Node n t1 t2) =
[n] ++ listout t1 ++ listout t2
Madras Christian College, 10 December 2003 – p.11
![Page 46: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/46.jpg)
Functions on recursive datatypes
� Define by induction on the structure of datatype� How many values are there in the tree?
size :: Btreeint -> Int
size Nil = 0
size (Node n t1 t2) = 1 + (size t1)
+ (size t2)
� List out all values in the tree
listout :: Btreeint -> [Int]
listout Nil = []
listout (Node n t1 t2) =
[n] ++ listout t1 ++ listout t2
Madras Christian College, 10 December 2003 – p.11
![Page 47: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/47.jpg)
Functions on recursive datatypes
� Define by induction on the structure of datatype� How many values are there in the tree?
size :: Btreeint -> Int
size Nil = 0
size (Node n t1 t2) = 1 + (size t1)
+ (size t2)
� List out all values in the tree
listout :: Btreeint -> [Int]
listout Nil = []
listout (Node n t1 t2) =
[n] ++ listout t1 ++ listout t2
Madras Christian College, 10 December 2003 – p.11
![Page 48: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/48.jpg)
Functions on recursive datatypes
� Define by induction on the structure of datatype� How many values are there in the tree?
size :: Btreeint -> Int
size Nil = 0
size (Node n t1 t2) = 1 + (size t1)
+ (size t2)
� List out all values in the tree
listout :: Btreeint -> [Int]
listout Nil = []
listout (Node n t1 t2) =
[n] ++ listout t1 ++ listout t2
Madras Christian College, 10 December 2003 – p.11
![Page 49: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/49.jpg)
Polymorphic recursive datatypes
� A binary tree to store arbitrary values at each node?
data Btree a =Nil |
Node a (Btree a) (Btree a)
� What if we want to use Btree a as a search tree
Values in the tree must have a natural ordering� Conditional polymorphism!
(Ord a) => data Btree a =Nil |
Node a (Btree a) (Btree a)
Madras Christian College, 10 December 2003 – p.12
![Page 50: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/50.jpg)
Polymorphic recursive datatypes
� A binary tree to store arbitrary values at each node?
data Btree a =Nil |
Node a (Btree a) (Btree a)
� What if we want to use Btree a as a search tree
Values in the tree must have a natural ordering� Conditional polymorphism!
(Ord a) => data Btree a =Nil |
Node a (Btree a) (Btree a)
Madras Christian College, 10 December 2003 – p.12
![Page 51: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/51.jpg)
Polymorphic recursive datatypes
� A binary tree to store arbitrary values at each node?
data Btree a =Nil |
Node a (Btree a) (Btree a)
� What if we want to use Btree a as a search tree
Values in the tree must have a natural ordering
� Conditional polymorphism!
(Ord a) => data Btree a =Nil |
Node a (Btree a) (Btree a)
Madras Christian College, 10 December 2003 – p.12
![Page 52: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/52.jpg)
Polymorphic recursive datatypes
� A binary tree to store arbitrary values at each node?
data Btree a =Nil |
Node a (Btree a) (Btree a)
� What if we want to use Btree a as a search tree
Values in the tree must have a natural ordering� Conditional polymorphism!
(Ord a) => data Btree a =Nil |
Node a (Btree a) (Btree a)
Madras Christian College, 10 December 2003 – p.12
![Page 53: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/53.jpg)
Polymorphic recursive datatypes . . .
� Built in list type is a polymorphic recursive datatype
data Mylist a = Emptylist |
Append a (Mylist a)
� Since lists are built in, they can use special symbols[] and : for constructors Emptylist and Append
Madras Christian College, 10 December 2003 – p.13
![Page 54: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/54.jpg)
Polymorphic recursive datatypes . . .
� Built in list type is a polymorphic recursive datatype
data Mylist a = Emptylist |
Append a (Mylist a)
� Since lists are built in, they can use special symbols[] and : for constructors Emptylist and Append
Madras Christian College, 10 December 2003 – p.13
![Page 55: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/55.jpg)
Polymorphic recursive datatypes . . .
� Built in list type is a polymorphic recursive datatype
data Mylist a = Emptylist |
Append a (Mylist a)
� Since lists are built in, they can use special symbols[] and : for constructors Emptylist and Append
Madras Christian College, 10 December 2003 – p.13
![Page 56: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/56.jpg)
Adding recursive datatypes to type classes
� Can inherit type classes from underlying type
data Btree a =Nil |
Node a (Btree a) (Btree a)deriving (Eq, Show)
Note: Not Eq (Btree a)
but Eq a => Eq (Btree a)
� Derived == checks that trees have same structure
6 •
8•
/= 6•
8 •
Madras Christian College, 10 December 2003 – p.14
![Page 57: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/57.jpg)
Adding recursive datatypes to type classes
� Can inherit type classes from underlying type
data Btree a =Nil |
Node a (Btree a) (Btree a)deriving (Eq, Show)
Note: Not Eq (Btree a)
but Eq a => Eq (Btree a)
� Derived == checks that trees have same structure
6 •
8•
/= 6•
8 •
Madras Christian College, 10 December 2003 – p.14
![Page 58: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/58.jpg)
Adding recursive datatypes to type classes
� Can inherit type classes from underlying type
data Btree a =Nil |
Node a (Btree a) (Btree a)deriving (Eq, Show)
Note: Not Eq (Btree a)
but Eq a => Eq (Btree a)
� Derived == checks that trees have same structure
6 •
8•
/= 6•
8 •
Madras Christian College, 10 December 2003 – p.14
![Page 59: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/59.jpg)
Adding recursive datatypes to type classes
� Can inherit type classes from underlying type
data Btree a =Nil |
Node a (Btree a) (Btree a)deriving (Eq, Show)
Note: Not Eq (Btree a)
but Eq a => Eq (Btree a)
� Derived == checks that trees have same structure
6 •
8•
/= 6•
8 •
Madras Christian College, 10 December 2003 – p.14
![Page 60: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/60.jpg)
Adding recursive datatypes to type classes
� Can inherit type classes from underlying type
data Btree a =Nil |
Node a (Btree a) (Btree a)deriving (Eq, Show)
Note: Not Eq (Btree a)
but Eq a => Eq (Btree a)
� Derived == checks that trees have same structure
6 •
8•
/= 6•
8 •
Madras Christian College, 10 December 2003 – p.14
![Page 61: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/61.jpg)
Adding recursive datatypes to type classes . . .
� Or we can define our own functions
instance (Eq a) => Eq (Btree a) wheret1 == t2 = (listout t1 == listout t2)
6 •
8•
==6•
8 •
because
[6,8] == [6,8]
Madras Christian College, 10 December 2003 – p.15
![Page 62: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/62.jpg)
Adding recursive datatypes to type classes . . .
� Or we can define our own functions
instance (Eq a) => Eq (Btree a) wheret1 == t2 = (listout t1 == listout t2)
6 •
8•
==6•
8 •
because
[6,8] == [6,8]
Madras Christian College, 10 December 2003 – p.15
![Page 63: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/63.jpg)
Adding recursive datatypes to type classes . . .
� Or we can define our own functions
instance (Eq a) => Eq (Btree a) wheret1 == t2 = (listout t1 == listout t2)
6 •
8•
==6•
8 •
because
[6,8] == [6,8]
Madras Christian College, 10 December 2003 – p.15
![Page 64: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/64.jpg)
Adding recursive datatypes to type classes . . .
� Or we can define our own functions
instance (Eq a) => Eq (Btree a) wheret1 == t2 = (listout t1 == listout t2)
6 •
8•
==6•
8 •
because
[6,8] == [6,8]
Madras Christian College, 10 December 2003 – p.15
![Page 65: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/65.jpg)
Adding recursive datatypes to type classes . . .
� Or we can define our own functions
instance (Eq a) => Eq (Btree a) wheret1 == t2 = (listout t1 == listout t2)
6 •
8•
==6•
8 •
because
[6,8] == [6,8]
Madras Christian College, 10 December 2003 – p.15
![Page 66: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/66.jpg)
Declarative programming with abstract datatypes
� Rotate right — transformation used to balance trees
x
•
y •
t1 t2
t3
⇒
y
•
x•
t1
t2 t3
� rotateright (Node x (Node y t1 t2) t3) =Node y t1 (Node x t2 t3)
Madras Christian College, 10 December 2003 – p.16
![Page 67: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/67.jpg)
Declarative programming with abstract datatypes
� Rotate right — transformation used to balance trees
x
•
y •
t1 t2
t3
⇒
y
•
x•
t1
t2 t3
� rotateright (Node x (Node y t1 t2) t3) =Node y t1 (Node x t2 t3)
Madras Christian College, 10 December 2003 – p.16
![Page 68: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/68.jpg)
Abstract datatypes
� Example Queues
� Stores sequence of values in FIFO fashion� Append items at the tail of queue� Want a datatype Queue a with functions
addq :: (Queue a) -> a -> (Queue a)
removeq :: (Queue a) -> (a,Queue a)
isemptyq :: (Queue a) -> Bool
emptyqueue :: (Queue a)
Madras Christian College, 10 December 2003 – p.17
![Page 69: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/69.jpg)
Abstract datatypes
� Example Queues� Stores sequence of values in FIFO fashion
� Append items at the tail of queue� Want a datatype Queue a with functions
addq :: (Queue a) -> a -> (Queue a)
removeq :: (Queue a) -> (a,Queue a)
isemptyq :: (Queue a) -> Bool
emptyqueue :: (Queue a)
Madras Christian College, 10 December 2003 – p.17
![Page 70: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/70.jpg)
Abstract datatypes
� Example Queues� Stores sequence of values in FIFO fashion� Append items at the tail of queue
� Want a datatype Queue a with functions
addq :: (Queue a) -> a -> (Queue a)
removeq :: (Queue a) -> (a,Queue a)
isemptyq :: (Queue a) -> Bool
emptyqueue :: (Queue a)
Madras Christian College, 10 December 2003 – p.17
![Page 71: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/71.jpg)
Abstract datatypes
� Example Queues� Stores sequence of values in FIFO fashion� Append items at the tail of queue� Want a datatype Queue a with functions
addq :: (Queue a) -> a -> (Queue a)
removeq :: (Queue a) -> (a,Queue a)
isemptyq :: (Queue a) -> Bool
emptyqueue :: (Queue a)
Madras Christian College, 10 December 2003 – p.17
![Page 72: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/72.jpg)
Abstract datatypes . . .
� Implement a queue as a list
� data Queue a = Qu [a]
� addq :: (Queue a) -> a -> (Queue a)addq (Qu l) d = Qu (d:l)
removeq :: (Queue a) -> (a,Queue a)removeq (Qu l)) = (last l,Qu (init l))
isemptyq :: (Queue a) -> Boolisemptyq (Qu []) = Trueisemptyq q = False
emptyqueue :: (Queue a)emptyqueue = Qu []
Madras Christian College, 10 December 2003 – p.18
![Page 73: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/73.jpg)
Abstract datatypes . . .
� Implement a queue as a list� data Queue a = Qu [a]
� addq :: (Queue a) -> a -> (Queue a)addq (Qu l) d = Qu (d:l)
removeq :: (Queue a) -> (a,Queue a)removeq (Qu l)) = (last l,Qu (init l))
isemptyq :: (Queue a) -> Boolisemptyq (Qu []) = Trueisemptyq q = False
emptyqueue :: (Queue a)emptyqueue = Qu []
Madras Christian College, 10 December 2003 – p.18
![Page 74: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/74.jpg)
Abstract datatypes . . .
� Implement a queue as a list� data Queue a = Qu [a]
� addq :: (Queue a) -> a -> (Queue a)addq (Qu l) d = Qu (d:l)
removeq :: (Queue a) -> (a,Queue a)removeq (Qu l)) = (last l,Qu (init l))
isemptyq :: (Queue a) -> Boolisemptyq (Qu []) = Trueisemptyq q = False
emptyqueue :: (Queue a)emptyqueue = Qu []
Madras Christian College, 10 December 2003 – p.18
![Page 75: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/75.jpg)
Modules
� Group together the definitions for Queue a in aseparate reusable module
� module Queue where
data Queue a = Qu [a] addq :: (Queue a)-> a -> (Queue a)...emptyqueue :: (Queue a) ...
� Use these definitions in another fileimport Queue
� How do we prevent unauthorized access to a queue?remsec :: (Queue a) -> (a,Queue a)remsec (Qu (x:y:l)) = (y, Qu (x:l))
Madras Christian College, 10 December 2003 – p.19
![Page 76: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/76.jpg)
Modules
� Group together the definitions for Queue a in aseparate reusable module
� module Queue where
data Queue a = Qu [a] addq :: (Queue a)-> a -> (Queue a)...emptyqueue :: (Queue a) ...
� Use these definitions in another fileimport Queue
� How do we prevent unauthorized access to a queue?remsec :: (Queue a) -> (a,Queue a)remsec (Qu (x:y:l)) = (y, Qu (x:l))
Madras Christian College, 10 December 2003 – p.19
![Page 77: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/77.jpg)
Modules
� Group together the definitions for Queue a in aseparate reusable module
� module Queue where
data Queue a = Qu [a] addq :: (Queue a)-> a -> (Queue a)...emptyqueue :: (Queue a) ...
� Use these definitions in another fileimport Queue
� How do we prevent unauthorized access to a queue?remsec :: (Queue a) -> (a,Queue a)remsec (Qu (x:y:l)) = (y, Qu (x:l))
Madras Christian College, 10 December 2003 – p.19
![Page 78: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/78.jpg)
Modules
� Group together the definitions for Queue a in aseparate reusable module
� module Queue where
data Queue a = Qu [a] addq :: (Queue a)-> a -> (Queue a)...emptyqueue :: (Queue a) ...
� Use these definitions in another fileimport Queue
� How do we prevent unauthorized access to a queue?remsec :: (Queue a) -> (a,Queue a)remsec (Qu (x:y:l)) = (y, Qu (x:l))
Madras Christian College, 10 December 2003 – p.19
![Page 79: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/79.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 80: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/80.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 81: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/81.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 82: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/82.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 83: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/83.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 84: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/84.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 85: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/85.jpg)
Modules . . .
� Restrict visibility outside module
moduleQueue(addq,removeq,isemptyq,emptyqueue)where ...
� Constructor Qu is not visible if you doimport Queue
� Can override imported function with local definition
All Haskell programs implicitly import Prelude
Redefine builtin functions usingimport Prelude hiding (max)
Madras Christian College, 10 December 2003 – p.20
![Page 86: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/86.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting
� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 87: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/87.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 88: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/88.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 89: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/89.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 90: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/90.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 91: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/91.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 92: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/92.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 93: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/93.jpg)
Rewriting revisted
� More than one expression may qualify for rewriting� sqr x = x*x
� sqr (4+3)
; sqr 7 ; 7*7 ; 49
; (4+3)*(4+3) ; (4+3)*7 ; 7*7 ; 49
� If there are multiple expressions to rewrite, Haskellchooses outermost expression
� Outermost reduction ≡ ‘Lazy” rewritingEvaluate argument to a function only when needed.
� “Eager” rewriting — evaluate arguments beforeevaluating function
Madras Christian College, 10 December 2003 – p.21
![Page 94: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/94.jpg)
Lazy rewriting
� Haskell evaluates arguments only when needed
power :: Float -> Int -> Float
power x n = if (n == 0) then 1.0
else x * (power x (n-1))
� power (8.0/0.0) 0 ; 1.0
Madras Christian College, 10 December 2003 – p.22
![Page 95: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/95.jpg)
Lazy rewriting
� Haskell evaluates arguments only when needed
power :: Float -> Int -> Float
power x n = if (n == 0) then 1.0
else x * (power x (n-1))
� power (8.0/0.0) 0 ; 1.0
Madras Christian College, 10 December 2003 – p.22
![Page 96: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/96.jpg)
Lazy rewriting
� Haskell evaluates arguments only when needed
power :: Float -> Int -> Float
power x n = if (n == 0) then 1.0
else x * (power x (n-1))
� power (8.0/0.0) 0 ; 1.0
Madras Christian College, 10 December 2003 – p.22
![Page 97: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/97.jpg)
Infinite lists!
� The following definition makes sense in Haskellfrom n = n : from (n+1)
from 2
; 2:(from 3); 2:(3:(from 4)); 2:(3:(4:(from 5))). . .
� Limit is the infinite list [2,3,4,5,...]� Haskell can (and will) generate it incrementally, till you
stop it, or it runs out of memory� Can write [2..] to denote [2,3,4,...]
Madras Christian College, 10 December 2003 – p.23
![Page 98: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/98.jpg)
Infinite lists!
� The following definition makes sense in Haskellfrom n = n : from (n+1)
from 2
; 2:(from 3); 2:(3:(from 4)); 2:(3:(4:(from 5))). . .
� Limit is the infinite list [2,3,4,5,...]� Haskell can (and will) generate it incrementally, till you
stop it, or it runs out of memory� Can write [2..] to denote [2,3,4,...]
Madras Christian College, 10 December 2003 – p.23
![Page 99: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/99.jpg)
Infinite lists!
� The following definition makes sense in Haskellfrom n = n : from (n+1)
from 2
; 2:(from 3); 2:(3:(from 4)); 2:(3:(4:(from 5))). . .
� Limit is the infinite list [2,3,4,5,...]� Haskell can (and will) generate it incrementally, till you
stop it, or it runs out of memory� Can write [2..] to denote [2,3,4,...]
Madras Christian College, 10 December 2003 – p.23
![Page 100: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/100.jpg)
Infinite lists!
� The following definition makes sense in Haskellfrom n = n : from (n+1)
from 2
; 2:(from 3); 2:(3:(from 4)); 2:(3:(4:(from 5))). . .
� Limit is the infinite list [2,3,4,5,...]
� Haskell can (and will) generate it incrementally, till youstop it, or it runs out of memory
� Can write [2..] to denote [2,3,4,...]
Madras Christian College, 10 December 2003 – p.23
![Page 101: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/101.jpg)
Infinite lists!
� The following definition makes sense in Haskellfrom n = n : from (n+1)
from 2
; 2:(from 3); 2:(3:(from 4)); 2:(3:(4:(from 5))). . .
� Limit is the infinite list [2,3,4,5,...]� Haskell can (and will) generate it incrementally, till you
stop it, or it runs out of memory
� Can write [2..] to denote [2,3,4,...]
Madras Christian College, 10 December 2003 – p.23
![Page 102: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/102.jpg)
Infinite lists!
� The following definition makes sense in Haskellfrom n = n : from (n+1)
from 2
; 2:(from 3); 2:(3:(from 4)); 2:(3:(4:(from 5))). . .
� Limit is the infinite list [2,3,4,5,...]� Haskell can (and will) generate it incrementally, till you
stop it, or it runs out of memory� Can write [2..] to denote [2,3,4,...]
Madras Christian College, 10 December 2003 – p.23
![Page 103: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/103.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 104: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/104.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 105: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/105.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry
� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 106: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/106.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 107: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/107.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]
� Transfer smallest number into list of primes anddelete all its multiples
� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 108: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/108.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples
� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 109: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/109.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples� Repeat second step
forever!
Madras Christian College, 10 December 2003 – p.24
![Page 110: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/110.jpg)
Why infinite lists?
� Can sometimes simplify a problem
� Consider the problem of computing the nth primenumber
� Idea: generate all prime numbers and wait for the nth
entry� The Sieve of Eratosthenes
� Start with [2,3,4,...]� Transfer smallest number into list of primes and
delete all its multiples� Repeat second step forever!
Madras Christian College, 10 December 2003 – p.24
![Page 111: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/111.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 112: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/112.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 113: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/113.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..]
;2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 114: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/114.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0]
;2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 115: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/115.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0])
;2:3:sieve [z | z <- [y <- [4..],mod y 2 > 0], mod z 3 > 0]
...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 116: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/116.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...
;2:3:sieve [z | z <- [5,7,9...],mod z 3 > 0]...
;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 117: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/117.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...
;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 118: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/118.jpg)
The Sieve of Eratosthenes
primes = sieve [2..]where sieve (x:l) =x : sieve [y | y <- l, mod y x > 0]
How does this work?
sieve [2..];2:sieve [y | y <- [3..], mod y 2 > 0];2:sieve (3:[y | y <- [4..], mod y 2 > 0]);2:3:sieve [z | z <- [y <- [4..],
mod y 2 > 0], mod z 3 > 0]...;2:3:sieve [z | z <- [5,7,9...],
mod z 3 > 0]...;2:3:sieve [5,7,11...] ;. . .
Madras Christian College, 10 December 2003 – p.25
![Page 119: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/119.jpg)
The nth prime
� We now have an infinite list primes of primes
� nthprime n = head (drop (n-1) primes)
� Drop the first n−1 numbers from primes
� To take the head of the rest, only need to compute onemore entry in the list
� Once “enough” has been computed, the rest ofprimes is ignored!
Madras Christian College, 10 December 2003 – p.26
![Page 120: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/120.jpg)
The nth prime
� We now have an infinite list primes of primes� nthprime n = head (drop (n-1) primes)
� Drop the first n−1 numbers from primes
� To take the head of the rest, only need to compute onemore entry in the list
� Once “enough” has been computed, the rest ofprimes is ignored!
Madras Christian College, 10 December 2003 – p.26
![Page 121: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/121.jpg)
The nth prime
� We now have an infinite list primes of primes� nthprime n = head (drop (n-1) primes)
� Drop the first n−1 numbers from primes
� To take the head of the rest, only need to compute onemore entry in the list
� Once “enough” has been computed, the rest ofprimes is ignored!
Madras Christian College, 10 December 2003 – p.26
![Page 122: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/122.jpg)
The nth prime
� We now have an infinite list primes of primes� nthprime n = head (drop (n-1) primes)
� Drop the first n−1 numbers from primes
� To take the head of the rest, only need to compute onemore entry in the list
� Once “enough” has been computed, the rest ofprimes is ignored!
Madras Christian College, 10 December 2003 – p.26
![Page 123: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/123.jpg)
The nth prime
� We now have an infinite list primes of primes� nthprime n = head (drop (n-1) primes)
� Drop the first n−1 numbers from primes
� To take the head of the rest, only need to compute onemore entry in the list
� Once “enough” has been computed, the rest ofprimes is ignored!
Madras Christian College, 10 December 2003 – p.26
![Page 124: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/124.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism� Modules with hiding and overriding for abstract
datatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 125: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/125.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs
� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism� Modules with hiding and overriding for abstract
datatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 126: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/126.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism� Modules with hiding and overriding for abstract
datatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 127: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/127.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism� Modules with hiding and overriding for abstract
datatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 128: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/128.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism
� Modules with hiding and overriding for abstractdatatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 129: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/129.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism� Modules with hiding and overriding for abstract
datatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 130: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/130.jpg)
Concluding remarks
� Functional programming provides a framework fordeclarative programming
� Provably correct programs� Rapid prototyping
� Haskell has a powerful typing mechanism
� Conditional polymorphism� Modules with hiding and overriding for abstract
datatypes
� Lazy evaluation permits infinite data structures
Madras Christian College, 10 December 2003 – p.27
![Page 131: Functional Programming in Haskell Part 2 : Abstract dataypes and ...](https://reader034.fdocuments.net/reader034/viewer/2022051522/58a2e0881a28ab735d8be355/html5/thumbnails/131.jpg)
For more information
Software and other resources
� http://www.haskell.org
Quick tutorial
� A Gentle Introduction to Haskellby Paul Hudak et al
Textbooks
� The Craft of Functional Programmingby Simon Thompson
� Introduction to Functional Programming in Haskellby Richard Bird
Madras Christian College, 10 December 2003 – p.28