Skip to content

Commit 8a602d6

Browse files
committed
reordered some functions, commented functionality
1 parent 7b58415 commit 8a602d6

File tree

2 files changed

+64
-24
lines changed

2 files changed

+64
-24
lines changed

theory/Quine.hs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
-- Print the string copy of the source code
12
main :: IO ()
23
main = do
3-
putStr $ unlines $ file
4-
putStr $ unlines $ dent <$> (wrap $ escape <$> file)
4+
putStr $ unlines $ file -- the program body
5+
putStr $ unlines $ dent <$> (wrap $ escape <$> file) -- the string copy
56

7+
-- Indent a line with 4 spaces
68
dent :: String -> String
79
dent = (" " ++)
810

11+
-- Make a list of strings resemble the lines of a list of strings in haskell syntax
912
wrap :: [String] -> [String]
10-
wrap ss = (pre ("[" ++) $ fil ("," ++) $ ss) ++ ["]"]
13+
wrap ss = (pre ("[" ++) -- prepend a '[' to the first line
14+
$ fil ("," ++) $ ss) -- prepend a ',' to all but the first line
15+
++ ["]"] -- append a line which contains a ']'
1116
where
1217
pre :: ([a] -> [a]) -> [[a]] -> [[a]]
1318
pre f [] = [f []]
@@ -16,6 +21,7 @@ wrap ss = (pre ("[" ++) $ fil ("," ++) $ ss) ++ ["]"]
1621
fil _ [] = []
1722
fil f (x:xs) = [x] ++ (f <$> xs)
1823

24+
-- Escape a string
1925
escape :: String -> String
2026
escape s = [qot] ++ (s >>= escChar) ++ [qot]
2127
where
@@ -27,18 +33,24 @@ escape s = [qot] ++ (s >>= escChar) ++ [qot]
2733
| chr == qot = [esc, qot]
2834
| otherwise = [chr]
2935

36+
-- The internal representation of the program's own source code
3037
file :: [String]
3138
file =
32-
["main :: IO ()"
39+
["-- Print the string copy of the source code"
40+
,"main :: IO ()"
3341
,"main = do"
34-
," putStr $ unlines $ file"
35-
," putStr $ unlines $ dent <$> (wrap $ escape <$> file)"
42+
," putStr $ unlines $ file -- the program body"
43+
," putStr $ unlines $ dent <$> (wrap $ escape <$> file) -- the string copy"
3644
,""
45+
,"-- Indent a line with 4 spaces"
3746
,"dent :: String -> String"
3847
,"dent = (\" \" ++)"
3948
,""
49+
,"-- Make a list of strings resemble the lines of a list of strings in haskell syntax"
4050
,"wrap :: [String] -> [String]"
41-
,"wrap ss = (pre (\"[\" ++) $ fil (\",\" ++) $ ss) ++ [\"]\"]"
51+
,"wrap ss = (pre (\"[\" ++) -- prepend a '[' to the first line"
52+
," $ fil (\",\" ++) $ ss) -- prepend a ',' to all but the first line"
53+
," ++ [\"]\"] -- append a line which contains a ']'"
4254
," where"
4355
," pre :: ([a] -> [a]) -> [[a]] -> [[a]]"
4456
," pre f [] = [f []]"
@@ -47,6 +59,7 @@ file =
4759
," fil _ [] = []"
4860
," fil f (x:xs) = [x] ++ (f <$> xs)"
4961
,""
62+
,"-- Escape a string"
5063
,"escape :: String -> String"
5164
,"escape s = [qot] ++ (s >>= escChar) ++ [qot]"
5265
," where"
@@ -58,6 +71,7 @@ file =
5871
," | chr == qot = [esc, qot]"
5972
," | otherwise = [chr]"
6073
,""
74+
,"-- The internal representation of the program's own source code"
6175
,"file :: [String]"
6276
,"file ="
6377
]

theory/QuineMid.hs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,58 @@
1+
-- Print the source code
12
main :: IO ()
23
main = do
3-
putStr $ unlines $ file !! 0
4-
putStr $ unlines $ dent <$> (rewrap $ wrap . (escape <$>) <$> file)
5-
putStr $ unlines $ file !! 1
4+
putStr $ unlines $ file !! 0 -- the leading half
5+
putStr $ unlines $ dent <$> (rewrap $ wrap . (escape <$>) <$> file) -- the string copy
6+
putStr $ unlines $ file !! 1 -- and the trailing half
67

8+
-- The internal representation of the program's own source code
79
file :: [[String]]
810
file =
9-
[["main :: IO ()"
11+
[["-- Print the source code"
12+
,"main :: IO ()"
1013
,"main = do"
11-
," putStr $ unlines $ file !! 0"
12-
," putStr $ unlines $ dent <$> (rewrap $ wrap . (escape <$>) <$> file)"
13-
," putStr $ unlines $ file !! 1"
14+
," putStr $ unlines $ file !! 0 -- the leading half"
15+
," putStr $ unlines $ dent <$> (rewrap $ wrap . (escape <$>) <$> file) -- the string copy"
16+
," putStr $ unlines $ file !! 1 -- and the trailing half"
1417
,""
18+
,"-- The internal representation of the program's own source code"
1519
,"file :: [[String]]"
1620
,"file ="
1721
]
1822
,[""
23+
,"-- Indent a line with 4 spaces"
1924
,"dent :: String -> String"
2025
,"dent = (\" \" ++)"
2126
,""
22-
,"rewrap :: [[String]] -> [String]"
23-
,"rewrap = concat . pre (pre (\"[\" ++)) . fil (pre (\",\" ++)) . app (app (++ \"]\"))"
24-
,""
27+
,"-- Perform some operation to the first list"
2528
,"pre :: ([a] -> [a]) -> [[a]] -> [[a]]"
2629
,"pre f [] = [f []]"
2730
,"pre f (x:xs) = [f x] ++ xs"
2831
,""
32+
,"-- Perform some operation to all but the first list"
2933
,"fil :: ([a] -> [a]) -> [[a]] -> [[a]]"
3034
,"fil _ [] = []"
3135
,"fil f (x:xs) = [x] ++ (f <$> xs)"
3236
,""
37+
,"-- Perform some operation to the last list"
3338
,"app :: ([a] -> [a]) -> [[a]] -> [[a]]"
3439
,"app f [] = [f []]"
3540
,"app f [x] = [f x]"
3641
,"app f (x:xs) = [x] ++ app f xs"
3742
,""
38-
,"wrap :: [String] -> [String]"
39-
,"wrap ss = (pre (\"[\" ++) $ fil (\",\" ++) $ ss) ++ [\"]\"]"
43+
,"-- Make a list of strings resemble the lines of a list of strings in haskell syntax"
44+
,"wrap ss = (pre (\"[\" ++) -- prepend a '[' to the first line"
45+
," $ fil (\",\" ++) $ ss) -- prepend a ',' to all but the first line"
46+
," ++ [\"]\"] -- append a line which contains a ']'"
47+
,""
48+
,"-- Make a list of lists of strings resemble the lines of a list of lists of strings in haskell syntax"
49+
,"rewrap :: [[String]] -> [String]"
50+
,"rewrap = concat -- Concatenate the lists"
51+
," . pre (pre (\"[\" ++)) -- Prepend a '[' to the first line of the first list"
52+
," . fil (pre (\",\" ++)) -- Prepend a ',' to the first line of all but the first list"
53+
," . app (app (++ \"]\")) -- Append a ']' to the last line of the last list"
4054
,""
55+
,"-- Escape a string"
4156
,"escape :: String -> String"
4257
,"escape s = [qot] ++ (s >>= escChar) ++ [qot]"
4358
," where"
@@ -50,28 +65,39 @@ file =
5065
," | otherwise = [chr]"
5166
]]
5267

68+
-- Indent a line with 4 spaces
5369
dent :: String -> String
5470
dent = (" " ++)
5571

56-
rewrap :: [[String]] -> [String]
57-
rewrap = concat . pre (pre ("[" ++)) . fil (pre ("," ++)) . app (app (++ "]"))
58-
72+
-- Perform some operation to the first list
5973
pre :: ([a] -> [a]) -> [[a]] -> [[a]]
6074
pre f [] = [f []]
6175
pre f (x:xs) = [f x] ++ xs
6276

77+
-- Perform some operation to all but the first list
6378
fil :: ([a] -> [a]) -> [[a]] -> [[a]]
6479
fil _ [] = []
6580
fil f (x:xs) = [x] ++ (f <$> xs)
6681

82+
-- Perform some operation to the last list
6783
app :: ([a] -> [a]) -> [[a]] -> [[a]]
6884
app f [] = [f []]
6985
app f [x] = [f x]
7086
app f (x:xs) = [x] ++ app f xs
7187

72-
wrap :: [String] -> [String]
73-
wrap ss = (pre ("[" ++) $ fil ("," ++) $ ss) ++ ["]"]
88+
-- Make a list of strings resemble the lines of a list of strings in haskell syntax
89+
wrap ss = (pre ("[" ++) -- prepend a '[' to the first line
90+
$ fil ("," ++) $ ss) -- prepend a ',' to all but the first line
91+
++ ["]"] -- append a line which contains a ']'
92+
93+
-- Make a list of lists of strings resemble the lines of a list of lists of strings in haskell syntax
94+
rewrap :: [[String]] -> [String]
95+
rewrap = concat -- Concatenate the lists
96+
. pre (pre ("[" ++)) -- Prepend a '[' to the first line of the first list
97+
. fil (pre ("," ++)) -- Prepend a ',' to the first line of all but the first list
98+
. app (app (++ "]")) -- Append a ']' to the last line of the last list
7499

100+
-- Escape a string
75101
escape :: String -> String
76102
escape s = [qot] ++ (s >>= escChar) ++ [qot]
77103
where

0 commit comments

Comments
 (0)