Haskell,
pasted
on Nov 8:
|
data Tree a = Node (Tree a) (Tree a) | Leaf a | Empty deriving (Show)
main = do
let my_tree = Node (Node (Leaf [1,2,4,9]) (Leaf [0,3,5,5,8,5])) (Leaf [2,7,5,5,2])
print(cc_2 my_tree)
print(cc_2 my_tree)
bb my_tree
aa my_tree
cc my_tree
print("Done")
cc tree = print(tree)
bb tree = print(fmap2 f $ tree)
aa tree = print(fmap3 h $ tree)
cc_2 tree = [ reduce (+) (fmap2 length tree) , reduce (min) (fmap2 minimum tree) , reduce (max) (fmap2 maximum tree) ]
f :: [Int] -> [Int]
f param = map mm param
mm :: Int -> Int
mm param = param + 2
h param = [length param,minimum param,maximum param]
instance MyFunctor Tree where
fmap2 ff (Node l r) = Node (fmap2 ff l) (fmap2 ff r)
fmap2 ff (Leaf x) = Leaf (ff x)
fmap3 ff (Node l r) = Node (fmap2 ff l) (fmap2 ff r)
fmap3 ff (Leaf x) = Leaf (ff x)
class MyFunctor ff where
fmap2 :: (a -> b) -> ff a -> ff b
fmap3 :: (a -> [Int]) -> ff a -> ff [Int]
reduce ff (Node l r) = ff (reduce ff l) (reduce ff r)
reduce ff (Leaf x) = x
|
Output:
|
[15,0,9]
[15,0,9]
Node (Node (Leaf [3,4,6,11]) (Leaf [2,5,7,7,10,7])) (Leaf [4,9,7,7,4])
Node (Node (Leaf [4,1,9]) (Leaf [6,0,8])) (Leaf [5,2,7])
Node (Node (Leaf [1,2,4,9]) (Leaf [0,3,5,5,8,5])) (Leaf [2,7,5,5,2])
"Done"
|
|