-
Notifications
You must be signed in to change notification settings - Fork 2
/
lecture.hs
94 lines (70 loc) · 1.74 KB
/
lecture.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
85
86
87
88
89
90
91
92
93
94
{-# LANGUAGE NoImplicitPrelude #-}
import System.IO (getLine, putStrLn)
import Prelude (IO, Int, Show, String, id, (+), (++))
data Bool = True | False
data Nat = Zero | Succ Nat
deriving (Show)
data List a = Cons a (List a) | Nil
deriving (Show)
listOfInts = Cons Zero (Cons (Succ Zero) (Cons (Succ (Succ Zero)) Nil))
data Pair a b = Pair a b
isEmpty :: List a -> Bool
isEmpty xs = case xs of
Nil -> True
Cons _ _ -> False
head :: List a -> a
head xs = case xs of
Cons x xs -> x
tail :: List a -> List a
tail xs = case xs of
Cons x xs -> xs
append :: List a -> List a -> List a
append xs ys = case xs of
Nil -> ys
(Cons x xs) -> Cons x (append xs ys)
class Monoid a where
multiply :: a -> a -> a
u :: a
instance Monoid (List a) where
multiply = append
u = Nil
instance Monoid String where
multiply = (++)
u = ""
instance Monoid Int where
multiply = (+)
u = 0
concat :: (Monoid m) => List m -> m
concat xs = case xs of
Nil -> u
Cons x xs -> multiply x (concat xs)
class Equality a where
equal :: a -> a -> Bool
class Functor f where
map :: (a -> b) -> f a -> f b
instance Functor List where
map :: (a -> b) -> List a -> List b
map f xs = case xs of
Nil -> Nil
(Cons x xs) -> Cons (f x) (map f xs)
data Maybe a = Just a | Nothing
deriving (Show)
instance Functor Maybe where
map :: (a -> b) -> Maybe a -> Maybe b
map f xs = case xs of
Nothing -> Nothing
Just x -> Just (f x)
class Monad m where
return :: a -> m a
bind :: m a -> (a -> m b) -> m b
join :: (Monad m) => m (m a) -> m a
join xxs = bind xxs id
instance Monad List where
return x = Cons x Nil
bind x f = case x of
Cons xs xss -> append (f xs) (bind xss f)
Nil -> Nil
main :: IO ()
main = do
x <- getLine
putStrLn x