-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek01Problems.hs
155 lines (107 loc) · 4.77 KB
/
Week01Problems.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
module Week01Problems where
import Week01
import Prelude hiding (Left, Right, reverse)
{----------------------------------------------------------------------}
{- Exercises -}
{----------------------------------------------------------------------}
{- In the questions below, replace 'undefined' with your answers. Use
GHCi to test them. -}
{- 1. Write a function: -}
isHorizontal :: Direction -> Bool
isHorizontal = undefined
{- that returns 'True' if the direction is 'Left' or 'Right', and
'False' otherwise. -}
{- 2. Write a function: -}
flipHorizontally :: Direction -> Direction
flipHorizontally = undefined
{- that flips horizontally (Left <-> Right, Up and Down stay the same). -}
{- 3. Rewrite 'equalDirections' to take a 'Pair Direction Direction' as
input: -}
pairOfEqualDirections :: Pair Direction Direction -> Bool
pairOfEqualDirections = undefined
{- 4. Define a datatype 'Triple a b c' for values that have three
components. Write functions 'get1of3 :: Triple a b c -> a',
'get2of3' and 'get3of3' that return the first, second and third
components. You will have to come up with the type signatures
for the second and third one. -}
{- 5. Pattern matching on specific characters is done by writing the
character to match. For example: -}
isA :: Char -> Bool
isA 'A' = True
isA _ = False
{- Write a function 'dropSpaces' :: [Char] -> [Char]' that drops
spaces from the start of a list of characters. For example, we
should have:
*Week01Problems> dropSpaces " hello"
"hello"
(Strings in Haskell are really lists of 'Char's, so you can use
pattern matching on them.) -}
dropSpaces :: [Char] -> [Char]
dropSpaces = undefined
{- 6. Using 'reverse' and 'dropSpaces', write a function that removes
spaces at the *end* of a list of characters. For example:
*Week01Problems> dropTrailingSpaces "hello "
"hello"
-}
dropTrailingSpaces :: [Char] -> [Char]
dropTrailingSpaces = undefined
{- 7. HTML escaping. When writing HTML, the characters '<', '&', and '>'
are special because they are used to represent tags and
entities. To have these characters display properly as
themselves in HTML they need to be replaced by their entity
versions:
'<' becomes '<' ("less than")
'>' becomes '>' ("greater than")
'&' becomes '&' ("ampersand")
Write a function that performs this replacement on a string. You
should have, for example,
Week01Problems*> htmlEscape "<not a tag>"
"<not a tag>"
-}
htmlEscape :: String -> String
htmlEscape = undefined
{- 8. The following datatype represents a piece of text marked up with
style information. -}
data Markup
= Text String -- ^ Some text
| Bold Markup -- ^ Some markup to be styled in bold
| Italic Markup -- ^ Some markup to be styled in italics
| Concat Markup Markup -- ^ Two pieces of markup to be displayed in sequence
deriving (Show, Eq)
{- Here is an example: -}
exampleMarkup :: Markup
exampleMarkup = Concat (Bold (Text "Delays")) (Concat (Text " are ") (Italic (Text "possible")))
{- Writing markup like this is tedious, especially when there are
lots of 'Concat's. Write a function that takes a list of
'Markup's and concatenates them all together using 'Concat'. -}
catMarkup :: [Markup] -> Markup
catMarkup = undefined
{- Another way of making the writing of Markup easier is the
automatic insertion of spaces. Write another function that
concatenates a list of 'Markup's putting spaces between them: -}
catMarkupSpaced :: [Markup] -> Markup
catMarkupSpaced = undefined
{- Sometimes we want to remove all formatting from a piece of
text. Write a function that removes all 'Bold' and 'Italic'
instructions from a piece of Markup, replacing them with their
underlying plain markup.
For example:
Week01Problems*> removeStyle exampleMarkup
Concat (Text "Delays") (Concat (Text " are ") (Text "possible"))
-}
removeStyle :: Markup -> Markup
removeStyle = undefined
{- Finally, we can 'render' our markup to HTML. Write a function that
converts 'Markup' to its HTML string representation, using
'<strong>..</strong>' for bold and '<em>...</em>' for
italics. Use the 'htmEscape' function from above to make sure
that 'Text' nodes are correctly converted to HTML.
For example:
Week01Problems*> markupToHTML exampleMarkup
"<strong>Delays</strong> are <em>possible</em>"
and
Week01Problems*> markupToHTML (Bold (Text "<&>"))
"<strong><&></strong>"
-}
markupToHTML :: Markup -> String
markupToHTML = undefined