-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_instances.hs
84 lines (65 loc) · 1.6 KB
/
list_instances.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{-# LANGUAGE NoImplicitPrelude #-}
import Prelude (
Applicative,
Functor,
Int,
Monad,
Monoid,
Semigroup,
Show,
fmap,
mappend,
mempty,
pure,
($),
(*),
(+),
(<*>),
(<>),
(>>=),
)
data List a = Empty | Cons a (List a)
deriving (Show)
instance Semigroup (List a) where
(<>) = append
instance Monoid (List a) where
mempty = Empty
instance Functor List where
fmap = map
instance Applicative List where
pure x = Cons x Empty
(<*>) Empty _ = Empty
(<*>) (Cons f fs) xs = mappend (fmap f xs) (fs <*> xs)
instance Monad List where
(>>=) list f = concat $ fmap f list
infixr 6 <+>
(<+>) :: a -> List a -> List a
(<+>) = Cons
append :: List a -> List a -> List a
append Empty list2 = list2
append (Cons x xs) list2 = x <+> append xs list2
length :: List a -> Int
length Empty = 0
length (Cons _ xs) = 1 + length xs
sum :: List Int -> Int
sum Empty = 0
sum (Cons x xs) = x + sum xs
product :: List Int -> Int
product Empty = 1
product (Cons x xs) = x * product xs
foldr :: (a -> b -> b) -> b -> List a -> b
foldr _ baseVal Empty = baseVal
foldr f baseVal (Cons x xs) = f x (foldr f baseVal xs)
foldl :: (b -> a -> b) -> b -> List a -> b
foldl _ baseVal Empty = baseVal
foldl f baseVal (Cons x xs) = foldl f (f baseVal x) xs
flip :: (a -> b -> c) -> b -> a -> c
flip f b a = f a b
reverse :: List a -> List a
reverse = foldl (flip Cons) Empty
map :: (a -> b) -> List a -> List b
map _ Empty = Empty
map f (Cons x xs) = Cons (f x) (map f xs)
concat :: List (List a) -> List a
concat = foldr append Empty
l1 = 1 <+> 2 <+> 3 <+> Empty