Skip to content

Commit 1533da1

Browse files
committed
made helpers more generic, split various functions
1 parent c173dc4 commit 1533da1

File tree

2 files changed

+56
-58
lines changed

2 files changed

+56
-58
lines changed

theory/Quine.hs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
main :: IO ()
22
main = do
3-
putStr $ unlines $ header
4-
putStr $ unlines $ dent <$> wrap header
3+
putStr $ unlines $ file
4+
putStr $ unlines $ dent <$> (wrap $ escape <$> file)
55

66
dent :: String -> String
77
dent = (" " ++)
88

99
wrap :: [String] -> [String]
10-
wrap ss = (fil "," $ pre "[" $ escape <$> ss) ++ ["]"]
10+
wrap ss = (pre ("[" ++) $ fil ("," ++) $ ss) ++ ["]"]
1111
where
12-
pre :: [a] -> [[a]] -> [[a]]
13-
pre x [] = [x]
14-
pre x (s:ss) = [x ++ s] ++ ss
15-
fil :: [a] -> [[a]] -> [[a]]
16-
fil x [] = []
17-
fil x (s:ss) = [s] ++ ((x ++) <$> ss)
12+
pre :: ([a] -> [a]) -> [[a]] -> [[a]]
13+
pre f [] = [f []]
14+
pre f (x:xs) = [f x] ++ xs
15+
fil :: ([a] -> [a]) -> [[a]] -> [[a]]
16+
fil _ [] = []
17+
fil f (x:xs) = [x] ++ (f <$> xs)
1818

1919
escape :: String -> String
2020
escape s = [qot] ++ (s >>= escChar) ++ [qot]
@@ -27,25 +27,25 @@ escape s = [qot] ++ (s >>= escChar) ++ [qot]
2727
| chr == qot = [esc, qot]
2828
| otherwise = [chr]
2929

30-
header :: [String]
31-
header =
30+
file :: [String]
31+
file =
3232
["main :: IO ()"
3333
,"main = do"
34-
," putStr $ unlines $ header"
35-
," putStr $ unlines $ dent <$> wrap header"
34+
," putStr $ unlines $ file"
35+
," putStr $ unlines $ dent <$> (wrap $ escape <$> file)"
3636
,""
3737
,"dent :: String -> String"
3838
,"dent = (\" \" ++)"
3939
,""
4040
,"wrap :: [String] -> [String]"
41-
,"wrap ss = (fil \",\" $ pre \"[\" $ escape <$> ss) ++ [\"]\"]"
41+
,"wrap ss = (pre (\"[\" ++) $ fil (\",\" ++) $ ss) ++ [\"]\"]"
4242
," where"
43-
," pre :: [a] -> [[a]] -> [[a]]"
44-
," pre x [] = [x]"
45-
," pre x (s:ss) = [x ++ s] ++ ss"
46-
," fil :: [a] -> [[a]] -> [[a]]"
47-
," fil x [] = []"
48-
," fil x (s:ss) = [s] ++ ((x ++) <$> ss)"
43+
," pre :: ([a] -> [a]) -> [[a]] -> [[a]]"
44+
," pre f [] = [f []]"
45+
," pre f (x:xs) = [f x] ++ xs"
46+
," fil :: ([a] -> [a]) -> [[a]] -> [[a]]"
47+
," fil _ [] = []"
48+
," fil f (x:xs) = [x] ++ (f <$> xs)"
4949
,""
5050
,"escape :: String -> String"
5151
,"escape s = [qot] ++ (s >>= escChar) ++ [qot]"
@@ -58,6 +58,6 @@ header =
5858
," | chr == qot = [esc, qot]"
5959
," | otherwise = [chr]"
6060
,""
61-
,"header :: [String]"
62-
,"header ="
61+
,"file :: [String]"
62+
,"file ="
6363
]

theory/QuineMid.hs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
main :: IO ()
22
main = do
3-
putStr $ unlines $ list !! 0
4-
putStr $ unlines $ dent <$> (rewrap $ wrap <$> list)
5-
putStr $ unlines $ list !! 1
3+
putStr $ unlines $ file !! 0
4+
putStr $ unlines $ dent <$> (rewrap $ wrap . (escape <$>) <$> file)
5+
putStr $ unlines $ file !! 1
66

7-
list :: [[String]]
8-
list =
7+
file :: [[String]]
8+
file =
99
[["main :: IO ()"
1010
,"main = do"
11-
," putStr $ unlines $ list !! 0"
12-
," putStr $ unlines $ dent <$> (rewrap $ wrap <$> list)"
13-
," putStr $ unlines $ list !! 1"
11+
," putStr $ unlines $ file !! 0"
12+
," putStr $ unlines $ dent <$> (rewrap $ wrap . (escape <$>) <$> file)"
13+
," putStr $ unlines $ file !! 1"
1414
,""
15-
,"list :: [[String]]"
16-
,"list ="
15+
,"file :: [[String]]"
16+
,"file ="
1717
]
1818
,[""
1919
,"dent :: String -> String"
2020
,"dent = (\" \" ++)"
2121
,""
2222
,"rewrap :: [[String]] -> [String]"
23-
,"rewrap [] = [\"[]\"]"
24-
,"rewrap (l:ls) = app \"]\" $ (pure l >>= pre \"[\") ++ (ls >>= pre \",\")"
23+
,"rewrap = concat . pre (pre (\"[\" ++)) . fil (pre (\",\" ++)) . app (app (++ \"]\"))"
2524
,""
26-
,"pre :: [a] -> [[a]] -> [[a]]"
27-
,"pre x [] = [x]"
28-
,"pre x (s:ss) = [x ++ s] ++ ss"
25+
,"pre :: ([a] -> [a]) -> [[a]] -> [[a]]"
26+
,"pre f [] = [f []]"
27+
,"pre f (x:xs) = [f x] ++ xs"
2928
,""
30-
,"fil :: [a] -> [[a]] -> [[a]]"
31-
,"fil x [] = []"
32-
,"fil x (s:ss) = [s] ++ ((x ++) <$> ss)"
29+
,"fil :: ([a] -> [a]) -> [[a]] -> [[a]]"
30+
,"fil _ [] = []"
31+
,"fil f (x:xs) = [x] ++ (f <$> xs)"
3332
,""
34-
,"app :: [a] -> [[a]] -> [[a]]"
35-
,"app x [] = [x]"
36-
,"app x [t] = [t ++ x]"
37-
,"app x (s:ss) = [s] ++ app x ss"
33+
,"app :: ([a] -> [a]) -> [[a]] -> [[a]]"
34+
,"app f [] = [f []]"
35+
,"app f [x] = [f x]"
36+
,"app f (x:xs) = [x] ++ app f xs"
3837
,""
3938
,"wrap :: [String] -> [String]"
40-
,"wrap ss = (fil \",\" $ pre \"[\" $ escape <$> ss) ++ [\"]\"]"
39+
,"wrap ss = (pre (\"[\" ++) $ fil (\",\" ++) $ ss) ++ [\"]\"]"
4140
,""
4241
,"escape :: String -> String"
4342
,"escape s = [qot] ++ (s >>= escChar) ++ [qot]"
@@ -55,24 +54,23 @@ dent :: String -> String
5554
dent = (" " ++)
5655

5756
rewrap :: [[String]] -> [String]
58-
rewrap [] = ["[]"]
59-
rewrap (l:ls) = app "]" $ (pure l >>= pre "[") ++ (ls >>= pre ",")
57+
rewrap = concat . pre (pre ("[" ++)) . fil (pre ("," ++)) . app (app (++ "]"))
6058

61-
pre :: [a] -> [[a]] -> [[a]]
62-
pre x [] = [x]
63-
pre x (s:ss) = [x ++ s] ++ ss
59+
pre :: ([a] -> [a]) -> [[a]] -> [[a]]
60+
pre f [] = [f []]
61+
pre f (x:xs) = [f x] ++ xs
6462

65-
fil :: [a] -> [[a]] -> [[a]]
66-
fil x [] = []
67-
fil x (s:ss) = [s] ++ ((x ++) <$> ss)
63+
fil :: ([a] -> [a]) -> [[a]] -> [[a]]
64+
fil _ [] = []
65+
fil f (x:xs) = [x] ++ (f <$> xs)
6866

69-
app :: [a] -> [[a]] -> [[a]]
70-
app x [] = [x]
71-
app x [t] = [t ++ x]
72-
app x (s:ss) = [s] ++ app x ss
67+
app :: ([a] -> [a]) -> [[a]] -> [[a]]
68+
app f [] = [f []]
69+
app f [x] = [f x]
70+
app f (x:xs) = [x] ++ app f xs
7371

7472
wrap :: [String] -> [String]
75-
wrap ss = (fil "," $ pre "[" $ escape <$> ss) ++ ["]"]
73+
wrap ss = (pre ("[" ++) $ fil ("," ++) $ ss) ++ ["]"]
7674

7775
escape :: String -> String
7876
escape s = [qot] ++ (s >>= escChar) ++ [qot]

0 commit comments

Comments
 (0)