-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeek 7
112 lines (75 loc) · 2.24 KB
/
Week 7
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--Assignment 1
module Misc where
-- (a)
match :: String -> Bool
match s = go s [] -- where [] is an empty stack
where
go [] [] = True --String is parsed, stack is empty
go [] _ = False --String is parsed, stack is not empty
--Parenthesis is opened
go ('(':s) stack = go s ('(':stack)
--Parenthesis is closed correctly
go (')':s) ('(':stack) = go s stack
--Parenthesis is closed incorrectly
go (')':s) _ = False
--Curly brace is opened
go ('{':s) stack = go s ('{':stack)
--Curly brace is closed correctly
go ('}':s) ('{':stack) = go s stack
--Curly brace is closed incorrectly
go ('}':s) _ = False
--Parse char
go (char:chars) stack = go chars stack
-- (b)
risers :: Ord a => [a] -> [[a]]
risers [] = []
risers [x] = [[x]]
risers (x:y:ys) = if x<=y
then
--Assignment 2
module Polynomials where
data Monomial a = Mono Integer [a]
deriving (Eq, Ord, Show)
type Poly a = [Monomial a]
data SymbExpr a
= Var a
| Lit Integer
| Add (SymbExpr a) (SymbExpr a)
| Mul (SymbExpr a) (SymbExpr a)
deriving Show
-- (a)
evalPoly :: (a -> Integer) -> Poly a -> Integer
evalPoly fpoly poly = go fpoly poly
where
go fpoly [] = 0
go fpoly (monomial:monomials) = (getInt monomial) * (foldr (*) 1 (map fpoly (getVars monomial))) + go fpoly monomials
where
getInt (Mono int vars) = int
getVars (Mono int vars) = vars
-- (b)
foldSymbExpr :: (a -> b) -> (Integer -> b) -> (b -> b -> b) -> (b -> b -> b) -> SymbExpr a -> b
foldSymbExpr fvar flit fadd fmul expr = go expr
where
go (Var v) = fvar v
go (Lit int) = flit int
go (Add a b) = fadd (go a) (go b)
go (Mul a b) = fmul (go a) (go b)
{- (c)
...
-}
-- (d)
toPoly :: SymbExpr a -> Poly a
toPoly = foldSymbExpr undefined undefined undefined undefined
--Assignment 3
module Regexp where
import Parsers
-- (a) define the type Regexp here
data Regexp = Lit Char | Seq Regexp Regexp | Or Regexp Regexp | Iter Regexp
-- (b)
str2regexp :: String -> Regexp
str2regexp = undefined
-- (c)
regexpParser :: String -> Parser ()
regexpParser = undefined
matches :: String -> String -> Bool
matches = undefined