Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures
|
|
- Basil Bennett
- 5 years ago
- Views:
Transcription
1 Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Rd, Chennai , India madhavan@cmi.ac.in madhavan Madras Christian College, 10 December 2003 p.1
2 Haskell review Program Collection of function definitions Madras Christian College, 10 December 2003 p.2
3 Haskell review Program Collection of function definitions Computation Rewriting using definitions Madras Christian College, 10 December 2003 p.2
4 Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types Madras Christian College, 10 December 2003 p.2
5 Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types isdigit :: Char -> Bool Madras Christian College, 10 December 2003 p.2
6 Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types isdigit :: Char -> Bool isdigit 0 = True isdigit 1 = True... isdigit 9 = True isdigit c = False Madras Christian College, 10 December 2003 p.2
7 Haskell review Program Collection of function definitions Computation Rewriting using definitions Functions are associated with input and output types isdigit :: Char -> Bool isdigit 0 = True isdigit 1 = True... isdigit 9 = True isdigit c = False isdigit c (c >= 0 && c <= 9 ) = True otherwise = False Madras Christian College, 10 December 2003 p.2
8 Haskell review... Basic collective type is a list Madras Christian College, 10 December 2003 p.3
9 Haskell review... Basic collective type is a list Define list functions by induction on structure Madras Christian College, 10 December 2003 p.3
10 Haskell review... Basic collective type is a list Define list functions by induction on structure Example Adding up a list of integers Madras Christian College, 10 December 2003 p.3
11 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) Madras Christian College, 10 December 2003 p.3
12 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 Madras Christian College, 10 December 2003 p.3
13 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 basic arithmetic operations +, -,... Madras Christian College, 10 December 2003 p.3
14 Today s agenda Adding new types Madras Christian College, 10 December 2003 p.4
15 Today s agenda Adding new types Defining abstract datatypes Provide an interface that hides the implementation Madras Christian College, 10 December 2003 p.4
16 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
17 User defined datatypes The data declaration adds new datatypes Madras Christian College, 10 December 2003 p.5
18 User defined datatypes The data declaration adds new datatypes Enumerated types data Signal = Red Yellow Green Madras Christian College, 10 December 2003 p.5
19 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 Madras Christian College, 10 December 2003 p.5
20 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 -> Bool stopwhen2 c (c == Red) = True otherwise = False Madras Christian College, 10 December 2003 p.5
21 User defined types and type classes stopwhen2 requires Eq Signal Madras Christian College, 10 December 2003 p.6
22 User defined types and type classes stopwhen2 requires Eq Signal How about nextlight :: Signal -> Signal nextlight Green = Yellow nextlight Yellow = Red nextlight Red = Green Madras Christian College, 10 December 2003 p.6
23 User defined types and type classes stopwhen2 requires Eq Signal How about nextlight :: Signal -> Signal nextlight Green = Yellow nextlight Yellow = Red nextlight Red = Green Displaying result of nextlight requires Show Signal Madras Christian College, 10 December 2003 p.6
24 User defined types and type classes stopwhen2 requires Eq Signal How about nextlight :: Signal -> Signal nextlight Green = Yellow nextlight Yellow = Red nextlight Red = Green Displaying result of nextlight requires Show 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
25 Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Madras Christian College, 10 December 2003 p.7
26 Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Madras Christian College, 10 December 2003 p.7
27 Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Red == Red, Red /= Yellow,... Madras Christian College, 10 December 2003 p.7
28 Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (Eq, Show, Ord) Fixes default values Red == Red, Red /= Yellow,... show Red = "Red", show Yellow = "Yellow",... Madras Christian College, 10 December 2003 p.7
29 Adding user defined types to type classes Simplest solution is data Signal = Red Yellow Green deriving (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
30 Adding user defined types to type classes... Or, provide your own functions Madras Christian College, 10 December 2003 p.8
31 Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) Madras Christian College, 10 December 2003 p.8
32 Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" Madras Christian College, 10 December 2003 p.8
33 Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" instance Ord Signal where Green <= Yellow = True Yellow <= Red = True Red <= Green = True x <= y = False Madras Christian College, 10 December 2003 p.8
34 Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" instance Ord Signal where Green <= Yellow = True Yellow <= Red = True Red <= Green = True x <= y = False In the class Ord, >, >=,... are defined in terms of <= Madras Christian College, 10 December 2003 p.8
35 Adding user defined types to type classes... Or, provide your own functions data Signal = Red Yellow Green deriving (Eq) instance Show Signal where show Yellow = "Yellow" show c = "Black" instance Ord Signal where Green <= Yellow = True Yellow <= Red = True Red <= Green = True x <= 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
36 Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Madras Christian College, 10 December 2003 p.9
37 Recursive datatypes A binary tree to store integers at each node data Btreeint = Nil Node Int Btreeint Btreeint Nil and Node are constructors Madras Christian College, 10 December 2003 p.9
38 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 Madras Christian College, 10 December 2003 p.9
39 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,... Madras Christian College, 10 December 2003 p.9
40 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 Madras Christian College, 10 December 2003 p.9
41 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 Madras Christian College, 10 December 2003 p.9
42 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
43 Recursive datatypes... Example The tree 6 would be written as 4 8 Node 6 (Node 4 Nil Nil) (Node 8 (Node 7 Nil Nil) Nil) 7 Madras Christian College, 10 December 2003 p.10
44 Functions on recursive datatypes Define by induction on the structure of datatype Madras Christian College, 10 December 2003 p.11
45 Functions on recursive datatypes Define by induction on the structure of datatype How many values are there in the tree? Madras Christian College, 10 December 2003 p.11
46 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) Madras Christian College, 10 December 2003 p.11
47 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 Madras Christian College, 10 December 2003 p.11
48 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
49 Polymorphic recursive datatypes A binary tree to store arbitrary values at each node? data Btree a = Nil Node a (Btree a) (Btree a) Madras Christian College, 10 December 2003 p.12
50 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 Madras Christian College, 10 December 2003 p.12
51 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 Madras Christian College, 10 December 2003 p.12
52 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
53 Polymorphic recursive datatypes... Built in list type is a polymorphic recursive datatype Madras Christian College, 10 December 2003 p.13
54 Polymorphic recursive datatypes... Built in list type is a polymorphic recursive datatype data Mylist a = Emptylist Append a (Mylist a) Madras Christian College, 10 December 2003 p.13
55 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
56 Adding recursive datatypes to type classes Can inherit type classes from underlying type Madras Christian College, 10 December 2003 p.14
57 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) Madras Christian College, 10 December 2003 p.14
58 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) Madras Christian College, 10 December 2003 p.14
59 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 Madras Christian College, 10 December 2003 p.14
60 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 /= 8 6 Madras Christian College, 10 December 2003 p.14
61 Adding recursive datatypes to type classes... Or we can define our own functions Madras Christian College, 10 December 2003 p.15
62 Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) Madras Christian College, 10 December 2003 p.15
63 Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) 6 8 == 8 6 because [6,8] == [6,8] Madras Christian College, 10 December 2003 p.15
64 Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) 6 8 == 8 6 because [6,8] == [6,8] Madras Christian College, 10 December 2003 p.15
65 Adding recursive datatypes to type classes... Or we can define our own functions instance (Eq a) => Eq (Btree a) where t1 == t2 = (listout t1 == listout t2) 6 8 == 8 6 because [6,8] == [6,8] Madras Christian College, 10 December 2003 p.15
66 Declarative programming with abstract datatypes Rotate right transformation used to balance trees x y y x t 3 t 1 t 1 t 2 t 2 t 3 Madras Christian College, 10 December 2003 p.16
67 Declarative programming with abstract datatypes Rotate right transformation used to balance trees x y y x t 3 t 1 t 1 t 2 t 2 t 3 rotateright (Node x (Node y t1 t2) t3) = Node y t1 (Node x t2 t3) Madras Christian College, 10 December 2003 p.16
68 Abstract datatypes Example Queues Madras Christian College, 10 December 2003 p.17
69 Abstract datatypes Example Queues Stores sequence of values in FIFO fashion Madras Christian College, 10 December 2003 p.17
70 Abstract datatypes Example Queues Stores sequence of values in FIFO fashion Append items at the tail of queue Madras Christian College, 10 December 2003 p.17
71 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
72 Abstract datatypes... Implement a queue as a list Madras Christian College, 10 December 2003 p.18
73 Abstract datatypes... Implement a queue as a list data Queue a = Qu [a] Madras Christian College, 10 December 2003 p.18
74 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) -> Bool isemptyq (Qu []) = True isemptyq q = False emptyqueue :: (Queue a) emptyqueue = Qu [] Madras Christian College, 10 December 2003 p.18
75 Modules Group together the definitions for Queue a in a separate reusable module Madras Christian College, 10 December 2003 p.19
76 Modules Group together the definitions for Queue a in a separate reusable module module Queue where data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a)... emptyqueue :: (Queue a)... Madras Christian College, 10 December 2003 p.19
77 Modules Group together the definitions for Queue a in a separate reusable module module Queue where data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a)... emptyqueue :: (Queue a)... Use these definitions in another file import Queue Madras Christian College, 10 December 2003 p.19
78 Modules Group together the definitions for Queue a in a separate reusable module module Queue where data Queue a = Qu [a] addq :: (Queue a) -> a -> (Queue a)... emptyqueue :: (Queue a)... Use these definitions in another file import 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
79 Modules... Restrict visibility outside module Madras Christian College, 10 December 2003 p.20
80 Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Madras Christian College, 10 December 2003 p.20
81 Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Madras Christian College, 10 December 2003 p.20
82 Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition Madras Christian College, 10 December 2003 p.20
83 Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition All Haskell programs implicitly import Prelude Madras Christian College, 10 December 2003 p.20
84 Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition All Haskell programs implicitly import Prelude Redefine builtin functions using import Prelude hiding (max) Madras Christian College, 10 December 2003 p.20
85 Modules... Restrict visibility outside module module Queue(addq,removeq,isemptyq,emptyqueue) where... Constructor Qu is not visible if you do import Queue Can override imported function with local definition All Haskell programs implicitly import Prelude Redefine builtin functions using import Prelude hiding (max) Madras Christian College, 10 December 2003 p.20
86 Rewriting revisted More than one expression may qualify for rewriting Madras Christian College, 10 December 2003 p.21
87 Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x Madras Christian College, 10 December 2003 p.21
88 Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) Madras Christian College, 10 December 2003 p.21
89 Rewriting revisted More than one expression may qualify for rewriting sqr x = x*x sqr (4+3) sqr 7 7*7 49 Madras Christian College, 10 December 2003 p.21
90 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 Madras Christian College, 10 December 2003 p.21
91 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, Haskell chooses outermost expression Madras Christian College, 10 December 2003 p.21
92 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, Haskell chooses outermost expression Outermost reduction Lazy rewriting Evaluate argument to a function only when needed. Madras Christian College, 10 December 2003 p.21
93 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, Haskell chooses outermost expression Outermost reduction Lazy rewriting Evaluate argument to a function only when needed. Eager rewriting evaluate arguments before evaluating function Madras Christian College, 10 December 2003 p.21
94 Lazy rewriting Haskell evaluates arguments only when needed Madras Christian College, 10 December 2003 p.22
95 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)) Madras Christian College, 10 December 2003 p.22
96 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) Madras Christian College, 10 December 2003 p.22
97 Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) Madras Christian College, 10 December 2003 p.23
98 Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 Madras Christian College, 10 December 2003 p.23
99 Infinite lists! The following definition makes sense in Haskell from n = n : from (n+1) from 2 2:(from 3) 2:(3:(from 4)) 2:(3:(4:(from 5)))... Madras Christian College, 10 December 2003 p.23
100 Infinite lists! The following definition makes sense in Haskell from 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,...] Madras Christian College, 10 December 2003 p.23
101 Infinite lists! The following definition makes sense in Haskell from 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 Madras Christian College, 10 December 2003 p.23
102 Infinite lists! The following definition makes sense in Haskell from 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
103 Why infinite lists? Can sometimes simplify a problem Madras Christian College, 10 December 2003 p.24
104 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Madras Christian College, 10 December 2003 p.24
105 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry Madras Christian College, 10 December 2003 p.24
106 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Madras Christian College, 10 December 2003 p.24
107 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Start with [2,3,4,...] Madras Christian College, 10 December 2003 p.24
108 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th entry The Sieve of Eratosthenes Start with [2,3,4,...] Transfer smallest number into list of primes and delete all its multiples Madras Christian College, 10 December 2003 p.24
109 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th 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 Madras Christian College, 10 December 2003 p.24
110 Why infinite lists? Can sometimes simplify a problem Consider the problem of computing the n th prime number Idea: generate all prime numbers and wait for the n th 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
111 The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] Madras Christian College, 10 December 2003 p.25
112 The Sieve of Eratosthenes primes = sieve [2..] where sieve (x:l) = x : sieve [y y <- l, mod y x > 0] How does this work? Madras Christian College, 10 December 2003 p.25
113 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..] Madras Christian College, 10 December 2003 p.25
114 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] Madras Christian College, 10 December 2003 p.25
115 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]) Madras Christian College, 10 December 2003 p.25
116 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]... Madras Christian College, 10 December 2003 p.25
117 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]... Madras Christian College, 10 December 2003 p.25
118 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
119 The n th prime We now have an infinite list primes of primes Madras Christian College, 10 December 2003 p.26
120 The n th prime We now have an infinite list primes of primes nthprime n = head (drop (n-1) primes) Madras Christian College, 10 December 2003 p.26
121 The n th 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 Madras Christian College, 10 December 2003 p.26
122 The n th 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 one more entry in the list Madras Christian College, 10 December 2003 p.26
123 The n th 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 one more entry in the list Once enough has been computed, the rest of primes is ignored! Madras Christian College, 10 December 2003 p.26
124 Concluding remarks Functional programming provides a framework for declarative programming Madras Christian College, 10 December 2003 p.27
125 Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Madras Christian College, 10 December 2003 p.27
126 Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Madras Christian College, 10 December 2003 p.27
127 Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Madras Christian College, 10 December 2003 p.27
128 Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Conditional polymorphism Madras Christian College, 10 December 2003 p.27
129 Concluding remarks Functional programming provides a framework for declarative programming Provably correct programs Rapid prototyping Haskell has a powerful typing mechanism Conditional polymorphism Modules with hiding and overriding for abstract datatypes Madras Christian College, 10 December 2003 p.27
130 Concluding remarks Functional programming provides a framework for declarative 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
131 For more information Software and other resources Quick tutorial A Gentle Introduction to Haskell by Paul Hudak et al Textbooks The Craft of Functional Programming by Simon Thompson Introduction to Functional Programming in Haskell by Richard Bird Madras Christian College, 10 December 2003 p.28
Functional Programming in Haskell Part I : Basics
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
More informationProgramming Language Concepts: Lecture 14
Programming Language Concepts: Lecture 14 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 14, 11 March 2009 Function programming
More informationLecture 19: Functions, Types and Data Structures in Haskell
The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming
More informationAdvanced features of Functional Programming (Haskell)
Advanced features of Functional Programming (Haskell) Polymorphism and overloading January 10, 2017 Monomorphic and polymorphic types A (data) type specifies a set of values. Examples: Bool: the type of
More informationCITS3211 FUNCTIONAL PROGRAMMING. 7. Lazy evaluation and infinite lists
CITS3211 FUNCTIONAL PROGRAMMING 7. Lazy evaluation and infinite lists Summary: This lecture introduces lazy evaluation and infinite lists in functional languages. cs123 notes: Lecture 19 R.L. While, 1997
More informationIntroduction to Programming, Aug-Dec 2006
Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components
More informationCSci 450: Org. of Programming Languages Overloading and Type Classes
CSci 450: Org. of Programming Languages Overloading and Type Classes H. Conrad Cunningham 27 October 2017 (after class) Contents 9 Overloading and Type Classes 1 9.1 Chapter Introduction.........................
More informationThe List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona
The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList
More informationAn introduction to functional programming. July 23, 2010
An introduction to functional programming July 23, 2010 About Outline About About What is functional programming? What is? Why functional programming? Why? is novel. is powerful. is fun. About A brief
More informationHaskell Overview III (3A) Young Won Lim 10/4/16
(3A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationPractical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.
Practical Practical An introduction to functional programming July 21, 2011 Contents Practical Practical is fun, and that s what it s all about! Even if seems strange to you at first, don t give up. Learning
More informationIntroduction to Programming, Aug-Dec 2008
Introduction to Programming, Aug-Dec 2008 Lecture 1, Monday 4 Aug 2008 Administrative matters Resource material Textbooks and other resource material for the course: The Craft of Functional Programming
More informationFunctional Programming
Functional Programming Overview! Functional vs. imperative programming! Fundamental concepts! Evaluation strategies! Pattern matching! Higher order functions! Lazy lists References! Richard Bird, Introduction
More informationHaskell 98 in short! CPSC 449 Principles of Programming Languages
Haskell 98 in short! n Syntax and type inferencing similar to ML! n Strongly typed! n Allows for pattern matching in definitions! n Uses lazy evaluation" F definition of infinite lists possible! n Has
More informationLogic - CM0845 Introduction to Haskell
Logic - CM0845 Introduction to Haskell Diego Alejandro Montoya-Zapata EAFIT University Semester 2016-1 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Haskell Semester
More informationHaskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza
Haskell 101 (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza Haskell 101: Contents Introduction Tutorial Homework Bibliography Haskell 101: Contents Introduction Tutorial Homework Bibliography Haskell
More informationLecture 5: Lazy Evaluation and Infinite Data Structures
Lecture 5: Lazy Evaluation and Infinite Data Structures Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense October 3, 2017 How does Haskell evaluate a
More informationn n Try tutorial on front page to get started! n spring13/ n Stack Overflow!
Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!
More informationHaskell Types COMP360
Haskell Types COMP360 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. Stan Kelly-Bootle British author, singer-songwriter and computer
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 08: Type Classes o o Review: What is a type class? Basic Type Classes: Eq, Ord, Enum, Integral,
More informationAlgebraic Types. Chapter 14 of Thompson
Algebraic Types Chapter 14 of Thompson Types so far Base types Int, Integer, Float, Bool, Char Composite types: tuples (t 1,t 2,,t n ) lists [t 1 ] functions (t 1 -> t 2 ) Algebraic types enumerated, product
More informationTopic 8: Lazy Evaluation
Topic 8: Lazy Evaluation 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 17.1, 17.2, 17.4, 17.8, 17.23, 17.25, 17.28, 17.29 Readings: Chapter
More informationUser-Defined Algebraic Data Types
72 Static Semantics User-Defined Types User-Defined Algebraic Data Types An algebraic data type declaration has the general form: data cx T α 1... α k = K 1 τ 11... τ 1k1... K n τ n1... τ nkn introduces
More informationThese notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without
These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create
More informationIntroduction to Programming: Lecture 3
Introduction to Programming: Lecture 3 K Narayan Kumar Chennai Mathematical Institute http://www.cmi.ac.in/~kumar 14 Aug 2012 Polymorphism in Haskell mylength [] = 0 mylength (x:xs) = 1 + mylength xs Polymorphism
More informationChapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction
Topics Chapter 15 Functional Programming Reduction and Currying Recursive definitions Local definitions Type Systems Strict typing Polymorphism Classes Booleans Characters Enumerations Tuples Strings 2
More informationCSC324 Principles of Programming Languages
CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 21, 2018 Last Class Types terminology Haskell s type system Currying Defining types Value constructors Algebraic data
More informationHaskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules
Haskell: From Basic to Advanced Part 2 Type Classes, Laziness, IO, Modules Qualified types In the types schemes we have seen, the type variables were universally quantified, e.g. ++ :: [a] -> [a] -> [a]
More informationCSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard
More informationCSc 372 Comparative Programming Languages
CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg
More informationHaskell Types, Classes, and Functions, Currying, and Polymorphism
1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the
More informationPROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions
PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product
More informationInductive Data Types
Inductive Data Types Lars-Henrik Eriksson Functional Programming 1 Original slides by Tjark Weber Lars-Henrik Eriksson (UU) Inductive Data Types 1 / 42 Inductive Data Types Today Today New names for old
More informationPROGRAMMING IN HASKELL. Chapter 2 - First Steps
PROGRAMMING IN HASKELL Chapter 2 - First Steps 0 The Hugs System Hugs is an implementation of Haskell 98, and is the most widely used Haskell system; The interactive nature of Hugs makes it well suited
More informationCSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona
1/43 CSc 372 Comparative Programming Languages 8 : Haskell Function Examples Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg Functions over Lists
More informationPROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions
PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}
More informationFall 2018 Lecture N
15-150 Fall 2018 Lecture N Tuesday, 20 November I m too lazy to figure out the correct number Stephen Brookes midterm2 Mean: 79.5 Std Dev: 18.4 Median: 84 today or, we could procrastinate lazy functional
More informationCS 457/557: Functional Languages
CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is
More informationProgramming Languages Fall 2013
Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy
More informationFunctional Programming. Overview. Topics. Recall λ-terms. Examples
Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search
More informationTree Equality: The Problem
Recall the tree data type we defined: Tree Equality: The Problem data LTree a = LLeaf a LBranch (LTree a) (LTree a) Suppose we want to write a function that determines if two trees are equal: treeeq (LLeaf
More informationStandard prelude. Appendix A. A.1 Classes
Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from
More informationFunctional Programming and Haskell
Functional Programming and Haskell Tim Dawborn University of Sydney, Australia School of Information Technologies Tim Dawborn Functional Programming and Haskell 1/22 What are Programming Paradigms? A programming
More informationLecture 2: List algorithms using recursion and list comprehensions
Lecture 2: List algorithms using recursion and list comprehensions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 12, 2017 Expressions, patterns
More informationCSCE 314 Programming Languages
CSCE 314 Programming Languages Haskell: Declaring Types and Classes Dr. Hyunyoung Lee 1 Outline Declaring Data Types Class and Instance Declarations 2 Defining New Types Three constructs for defining types:
More informationTyped Racket: Racket with Static Types
Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type
More informationSometimes it s good to be lazy
Chapter 8 Sometimes it s good to be lazy Typically, languages such as OCaml (SML, Lisp or Scheme) evaluate expressions by a call-by-value discipline. All variables in OCaml are bound by value, which means
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Introduc;on to Hakell Lesson 27! 1 The origins: ML programming
More informationRecursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions
Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin
More informationTHE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE
UDC 51:681.3 Ján Kollár THE CONCEPTION AND APPLICATION OF PFL : A PROCESS FUNCTIONAL PROGRAMMING LANGUAGE A new process functional programming paradigm and its application in PFL a process functional programming
More informationTopic 6: Partial Application, Function Composition and Type Classes
Topic 6: Partial Application, Function Composition and Type Classes Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 11.11, 11.12 12.30, 12.31,
More informationTopic 6: Partial Application, Function Composition and Type Classes
Topic 6: Partial Application, Function Composition and Type Classes 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 11.11, 11.12 12.30, 12.31,
More informationHaskell An Introduction
Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First
More informationIntroduction to Functional Programming in Haskell 1 / 56
Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order
More informationProgramming Language Concepts: Lecture 19
Programming Language Concepts: Lecture 19 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 19, 01 April 2009 Adding types
More informationSML A F unctional Functional Language Language Lecture 19
SML A Functional Language Lecture 19 Introduction to SML SML is a functional programming language and acronym for Standard d Meta Language. SML has basic data objects as expressions, functions and list
More informationOverview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping
Overview Declarative Languages D7012E Lecture 4: The Haskell type system Fredrik Bengtsson / Johan Nordlander Overloading & polymorphism Type classes instances of type classes derived type classes Type
More informationHaskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)
Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes Henrik Nilsson University of Nottingham, UK What is the type of (==)? E.g. the following both work: 1 == 2 a == b I.e., (==) can be used to compare
More informationFunctional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph
Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search
More informationCS 440: Programming Languages and Translators, Spring 2019 Mon
Haskell, Part 4 CS 440: Programming Languages and Translators, Spring 2019 Mon 2019-01-28 More Haskell Review definition by cases Chapter 6: Higher-order functions Revisit currying map, filter Unnamed
More informationHaskell Types COMP360
Haskell Types COMP360 No computer has ever been designed that is ever aware of what it's doing; but most of the time, we aren't either. Marvin Minsky Haskell Programming Assignment A Haskell programming
More informationHomework 1: Functional Programming, Haskell
Com S 541 Programming Languages 1 September 25, 2005 Homework 1: Functional Programming, Haskell Due: problems 1 and 3, Thursday, September 1, 2005; problems 4-9, Thursday, September 15, 2005; remaining
More informationCSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides
CSC324- TUTORIAL 5 ML Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides Assignment 1 2 More questions were added Questions regarding the assignment? Starting ML Who am I? Shems Saleh
More informationHaskell & functional programming, some slightly more advanced stuff. Matteo Pradella
Haskell & functional programming, some slightly more advanced stuff Matteo Pradella pradella@elet.polimi.it IEIIT, Consiglio Nazionale delle Ricerche & DEI, Politecnico di Milano PhD course @ UniMi - Feb
More informationCSE 3302 Programming Languages Lecture 8: Functional Programming
CSE 3302 Programming Languages Lecture 8: Functional Programming (based on the slides by Tim Sheard) Leonidas Fegaras University of Texas at Arlington CSE 3302 L8 Spring 2011 1 Functional Programming Languages
More informationCSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.
CSC312 Principles of Programming Languages : Functional Programming Language Overview of Functional Languages They emerged in the 1960 s with Lisp Functional programming mirrors mathematical functions:
More informationINTRODUCTION TO HASKELL
INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be
More informationOBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe
OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++
More informationFunctional Programming I *** Functional Programming and Interactive Theorem Proving. Ulrich Berger Michaelmas Term 2006
Functional Programming I *** Functional Programming and Interactive Theorem Proving Ulrich Berger Michaelmas Term 2006 2 *** Room 306 (Faraday Tower) Phone 513380 Fax 295708 u.berger@swansea.ac.uk http://www-compsci.swan.ac.uk/
More informationCS 360: Programming Languages Lecture 12: More Haskell
CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due
More informationIt is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
Chapter 14 Functional Programming Programming Languages 2nd edition Tucker and Noonan It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis
More informationCSc 520. Principles of Programming Languages 11: Haskell Basics
CSc 520 Principles of Programming Languages 11: Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg April
More informationCourse year Typeclasses and their instances
Course year 2016-2017 Typeclasses and their instances Doaitse Swierstra and Atze Dijkstra with extra s Utrecht University September 29, 2016 1. The basics 2 Overloading versus parametric polymorphism 1
More informationA general introduction to Functional Programming using Haskell
A general introduction to Functional Programming using Haskell Matteo Rossi Dipartimento di Elettronica e Informazione Politecnico di Milano rossi@elet.polimi.it 1 Functional programming in a nutshell
More informationProgramming in Haskell Aug-Nov 2015
Programming in Haskell Aug-Nov 2015 LECTURE 14 OCTOBER 1, 2015 S P SURESH CHENNAI MATHEMATICAL INSTITUTE Enumerated data types The data keyword is used to define new types data Bool = False True data Day
More informationImplementing an abstract datatype. Linked lists and queues
Computer Programming Implementing an abstract datatype. Linked lists and queues Marius Minea marius@cs.upt.ro 19 December 2016 Review: compilation basics Briefly: Compiler translates source code to executable
More informationProgramming Language Concepts, CS2104 Lecture 7
Reminder of Last Lecture Programming Language Concepts, CS2104 Lecture 7 Tupled Recursion Exceptions Types, ADT, Haskell, Components 5th Oct 2007 CS2104, Lecture 7 1 Overview 5th Oct 2007 CS2104, Lecture
More informationHaskell: From Basic to Advanced. Part 3 A Deeper Look into Laziness
Haskell: From Basic to Advanced Part 3 A Deeper Look into Laziness Haskell is a lazy language Laziness again A particular function argument is only evaluated when it is needed, and if it is needed then
More informationParallel Haskell on MultiCores and Clusters
Parallel Haskell on MultiCores and Clusters (part of Advanced Development Techniques) Hans-Wolfgang Loidl School of Mathematical and Computer Sciences Heriot-Watt University, Edinburgh (presented at the
More informationInformatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh
Informatics 1 Functional Programming Lecture 7 Map, filter, fold Don Sannella University of Edinburgh Part I Map Squares *Main> squares [1,-2,3] [1,4,9] squares :: [Int] -> [Int] squares xs [ x*x x
More informationFunctional Logic Programming Language Curry
Functional Logic Programming Language Curry Xiang Yin Department of Computer Science McMaster University November 9, 2010 Outline Functional Logic Programming Language 1 Functional Logic Programming Language
More informationProgramming Paradigms
PP 2017/18 Unit 12 Functions and Data Types in Haskell 1/45 Programming Paradigms Unit 12 Functions and Data Types in Haskell J. Gamper Free University of Bozen-Bolzano Faculty of Computer Science IDSE
More informationHaskell Overview II (2A) Young Won Lim 8/9/16
(2A) Copyright (c) 2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published
More informationCSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona
1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/40 The Hugs Interpreter The
More informationCSc 372 Comparative Programming Languages. 4 : Haskell Basics
CSc 372 Comparative Programming Languages 4 : Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg August 23, 2011
More informationAdvanced Topics in Programming Languages Lecture 2 - Introduction to Haskell
Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell Ori Bar El Maxim Finkel 01/11/17 1 History Haskell is a lazy, committee designed, pure functional programming language that
More informationStructural polymorphism in Generic Haskell
Structural polymorphism in Generic Haskell Andres Löh andres@cs.uu.nl 5 February 2005 Overview About Haskell Genericity and other types of polymorphism Examples of generic functions Generic Haskell Overview
More informationOverloading, Type Classes, and Algebraic Datatypes
Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827
More informationThe Typed Racket Guide
The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24! Type inference in ML / Haskell 1 Type Checking vs
More informationLecture 1 August 9, 2017
Programming in Haskell S P Suresh http://www.cmi.ac.in/~spsuresh Lecture 1 August 9, 2017 Administrative Mondays and Wednesdays at 9.10 am at Lecture Hall 6 Evaluation: Quizzes, 4 5 programming assignments,
More informationSolution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors
Solution sheet 1 Introduction Please note that there can be other solutions than those listed in this document. This is a literate Haskell file which is available as PDF, as well as literate Haskell source
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationCOSE212: Programming Languages. Lecture 3 Functional Programming in OCaml
COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of
More informationExercise 1 ( = 22 points)
1 Exercise 1 (4 + 3 + 4 + 5 + 6 = 22 points) The following data structure represents polymorphic lists that can contain values of two types in arbitrary order: data DuoList a b = C a (DuoList a b) D b
More informationLecture 10 September 11, 2017
Programming in Haskell S P Suresh http://www.cmi.ac.in/~spsuresh Lecture 10 September 11, 2017 Combining elements sumlist :: [Int] -> Int sumlist [] = 0 sumlist (x:xs) = x + (sumlist xs) multlist :: [Int]
More informationIntroduction to Programming: Lecture 10
Introduction to Programming: Lecture 10 K Narayan Kumar Chennai Mathematical Institute http://www.cmi.ac.in/~kumar 10 Sep 2012 Organizing functions as Modules Organize functions into modules. Organizing
More informationThis example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator
1 2 This example highlights the difference between imperative and functional programming. The imperative programming solution is based on an accumulator (total) and a counter (i); it works by assigning
More informationShell CSCE 314 TAMU. Functions continued
1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in
More information