-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek01Lecture2.hs
99 lines (64 loc) · 2.13 KB
/
Week01Lecture2.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
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module Week01Lecture2 where
import Data.List
-- Talk: In the Engine Room of LLMs
-- By: Satnam Singh (Groq Inc)
-- Where: RC513 Wednesday 11am 2nd October
data Markup
= Text String
| Bold Markup
| Italic Markup
| Concat Markup Markup
deriving (Show, Eq)
-- Pandoc tool
-- Example?
example :: Markup
example = Concat (Text "Hello") (Concat (Text " ") (Bold (Text "world")))
-- Hello **world**
-- Hello <strong>world</strong>
-- catMarkup
catMarkup :: [Markup] -> Markup
catMarkup [] = Text ""
catMarkup [x] = x
catMarkup (x : xs) = Concat x (catMarkup xs)
-- catMarkupSpaced [Text "hello", Text "world"]
-- Concat (Text "hello") (Concat (Text " ") (Text "world"))
catMarkupSpaced :: [Markup] -> Markup
catMarkupSpaced [] = Text ""
catMarkupSpaced [x] = x
catMarkupSpaced (x : xs) = Concat x (Concat (Text " ") (catMarkupSpaced xs))
catMarkupSpaced_v2 :: [Markup] -> Markup
catMarkupSpaced_v2 xs = catMarkup (intersperse (Text " ") xs)
-- = catMarkup . intersperse (Text " ")
-- (.) :: (b -> c) -> (a -> b) -> (a -> c)
-- (g . f) x = g (f x)
(|>) :: (a -> b) -> (b -> c) -> (a -> c)
(f |> g) x = g (f x)
sepBy :: Markup -> [Markup] -> Markup
sepBy separator = intersperse separator |> catMarkup
list :: [Markup] -> Markup
list xs = Concat (Text "[") (Concat (sepBy (Text ", ") xs) (Text "]"))
between :: Markup -> Markup -> (Markup -> Markup)
between l r xs = Concat l (Concat xs r)
bracket = between (Text "[") (Text "]")
strings :: [Markup] -> Markup
strings = map (between (Text "\"") (Text "\"")) |> list
-- markupToHTML
-- markupToHTML :: Markup -> String
-- "Bob" --;
-- DROP TABLE users;
-- "Little Bobby Tables"
strong :: [HTML] -> HTML
strong htmls = HEl "strong" htmls
em htmls = HEl "em" htmls
data HTML = HText String
| HEl String [HTML]
-- | HConcat HTML HTML
deriving (Show, Eq)
markupToHTML :: Markup -> [HTML]
markupToHTML (Text s) = [HText s]
markupToHTML (Bold m) = [strong (markupToHTML m)]
markupToHTML (Italic m) = [em (markupToHTML m)]
markupToHTML (Concat m1 m2) =
markupToHTML m1 ++ markupToHTML m2
-- escapeHTML