added practicum files, updated gitignore
[fp1415.git] / files / practicum / BinTreeMapEnFold.icl
1 module BinTreeMapEnFold
2
3 import StdEnv
4
5 :: BTree a = Tip a | Bin (BTree a) (BTree a)
6
7 testboom = Bin (Bin (Bin (Tip 1) (Tip 2)) (Tip 3)) (Bin (Bin (Tip 4) (Tip 5)) (Bin (Tip 6) (Bin (Tip 7) (Tip 8))))
8
9 mapbtree :: (a -> b) (BTree a) -> BTree b
10 mapbtree f (Tip a) = Tip (f a)
11 mapbtree f (Bin t1 t2) = Bin (mapbtree f t1) (mapbtree f t2)
12
13 foldbtree :: (a a -> a) (BTree a) -> a
14 foldbtree f (Tip a) = a
15 foldbtree f (Bin t1 t2) = f (foldbtree f t1) (foldbtree f t2)
16
17 f1 :: // meest algemene type
18 f1 = foldbtree (+)
19
20 f2 :: // meest algemene type
21 f2 = foldbtree (+) o (mapbtree (const 1))
22
23 f3 :: // meest algemene type
24 f3 = foldbtree (\x y -> 1 + max x y) o (mapbtree (const 0))
25
26 f4 :: // meest algemene type
27 f4 = foldbtree (++) o (mapbtree (\x -> [x]))
28
29 Start :: (Int,Char,Int,Char,Int,Char,[Int],Char)
30 Start = (f1 testboom, '\n'
31 ,f2 testboom, '\n'
32 ,f3 testboom, '\n'
33 ,f4 testboom, '\n'
34 )