-
Notifications
You must be signed in to change notification settings - Fork 0
/
livecoding1_20140710.hs
55 lines (36 loc) · 1.02 KB
/
livecoding1_20140710.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE DeriveFunctor #-}
-- functor
-- class Functor f where
-- fmap ::( a -> b) -> f a -> f b
-- [a] = [] a -- f = [] a = a
-- instance Functor [] where
-- fmap = map
-- map :: (a->b) -> [a] -> [b]
-- data Maybe a = Nothing | Just a
-- instance Functor Maybe where
-- -- fmap :: (a->b) -> Maybe a -> Maybe b
-- fmap f Nothing = Nothing
-- fmap f (Just x) = Just (f x)
data Tree a = Leaf a
| Node (Tree a) (Tree a)
deriving (Show, Functor)
fmap' f (Leaf x) = Leaf (f x)
fmap' f (Node l r) = Node (fmap f l) (fmap f r)
-- class Show a where
-- show :: a -> String
-- instance Show (Tree a) where
-- show
testtree :: Tree Int
testtree = Node (Node (Leaf 3) (Leaf 4)) (Node (Leaf 5) (Leaf 7))
func :: Int -> String
func x = show x ++ "!"
main :: IO ()
main = do
let lst= [1,2,3,4] :: [Int]
print (fmap (\x->x+1) lst)
let may = Just 1 :: Maybe Int
print (fmap (\x->x+1) may)
print testtree
print (func 3)
print (fmap func testtree)
print (fmap' func testtree)