comp 205: comparative programming languages
DESCRIPTION
Comp 205: Comparative Programming Languages. More User-Defined Types Tree types Catamorphisms Lecture notes, exercises, etc., can be found at: www.csc.liv.ac.uk/~grant/Teaching/COMP205/. Recursive Type Definitions. Some recursively-defined types:. data Nat = Zero | Succ Nat - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/1.jpg)
Comp 205:Comparative Programming
Languages
More User-Defined Types
• Tree types• Catamorphisms
Lecture notes, exercises, etc., can be found at:
www.csc.liv.ac.uk/~grant/Teaching/COMP205/
![Page 2: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/2.jpg)
Recursive Type Definitions
Some recursively-defined types:
data Nat = Zero | Succ Nat
data List a = Nil | Cons a (List a)
data BTree a = Leaf a | Fork (BTree a) (BTree a)
![Page 3: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/3.jpg)
BTree Int
Leaf
Fork
Fork
Fork
Leaf
Leaf
Leaf
2
9
51
![Page 4: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/4.jpg)
Recursive Functions
Some recursive functions on Nat:
data Nat = Zero | Succ Nat
-- convert "caveman notation"-- to decimal
value :: Nat -> Int
value Zero = 0value (Succ n) = 1 + value n
![Page 5: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/5.jpg)
More Recursive Functions
Some more recursive functions on Nat:
data Nat = Zero | Succ Nat
nplus :: Nat Nat -> Natnplus m Zero = mnplus m (Succ n) = Succ (nplus m n)
ntimes :: Nat Nat -> Natntimes m Zero = Zerontimes m (Succ n) = nplus m (ntimes m n)
![Page 6: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/6.jpg)
Recursive Functions on BTrees
data BTree a = Leaf a | Fork (BTree a) (BTree a)
sumAll :: BTree Int -> IntsumAll (Leaf n) = nsumAll (Fork t1 t2) = (sumAll t1)+(sumAll t2)
leaves :: BTree a -> [a]leaves (Leaf x) = [x]leaves (Fork t1 t2) = (leaves t1)++(leaves t2)
![Page 7: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/7.jpg)
Recursive Functions on BTrees
data BTree a = Leaf a | Fork (BTree a) (BTree a)
bTree_Map :: (a -> b) -> (BTree a) -> (BTree b)
bTree_Map f (Leaf x) = Leaf (f x)bTree_Map f (Fork t1 t2) = Fork (bTree_Map f t1) (bTree_Map f t2)
![Page 8: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/8.jpg)
Recursive Functions on BTrees
data BTree a = Leaf a | Fork (BTree a) (BTree a)
BTree_Reduce :: (a -> b) -> (b -> b -> b) -> (BTree a) -> b
bTree_Reduce f g (Leaf x) = f xbTree_Reduce f g (Fork t1 t2) = g (bTree_Reduce f g t1) (bTree_Reduce f g t2)
![Page 9: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/9.jpg)
Using Reduce
sumAll = bTree_Reduce id (+) where id x = x
leaves = bTree_Reduce (:[]) (++)
bTree_Map f = bTree_Reduce (Leaf.f) Fork
The recursive functions above can all beexpressed as reductions:
![Page 10: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/10.jpg)
Catamorphisms
bTree_Reduce f g is a catamorphism:
- it replaces constructors with functions
bTree_Reduce f g replaces
• Leaf with f
• Fork with g
![Page 11: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/11.jpg)
Replacing Constructors
f
g
g
g
f
f
f
2
9
51
Leaf
Fork
Fork
Fork
Leaf
Leaf
Leaf
2
9
51
![Page 12: Comp 205: Comparative Programming Languages](https://reader036.vdocuments.us/reader036/viewer/2022082711/56813095550346895d967248/html5/thumbnails/12.jpg)
Summary
• Recursive types
• Catamorphisms
Next: Infinite lists and lazy evaluation