1
+ -- Print the source code
1
2
main :: IO ()
2
3
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
6
7
8
+ -- The internal representation of the program's own source code
7
9
file :: [[String ]]
8
10
file =
9
- [[" main :: IO ()"
11
+ [[" -- Print the source code"
12
+ ," main :: IO ()"
10
13
," 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 "
14
17
," "
18
+ ," -- The internal representation of the program's own source code"
15
19
," file :: [[String]]"
16
20
," file ="
17
21
]
18
22
,[" "
23
+ ," -- Indent a line with 4 spaces"
19
24
," dent :: String -> String"
20
25
," dent = (\" \" ++)"
21
26
," "
22
- ," rewrap :: [[String]] -> [String]"
23
- ," rewrap = concat . pre (pre (\" [\" ++)) . fil (pre (\" ,\" ++)) . app (app (++ \" ]\" ))"
24
- ," "
27
+ ," -- Perform some operation to the first list"
25
28
," pre :: ([a] -> [a]) -> [[a]] -> [[a]]"
26
29
," pre f [] = [f []]"
27
30
," pre f (x:xs) = [f x] ++ xs"
28
31
," "
32
+ ," -- Perform some operation to all but the first list"
29
33
," fil :: ([a] -> [a]) -> [[a]] -> [[a]]"
30
34
," fil _ [] = []"
31
35
," fil f (x:xs) = [x] ++ (f <$> xs)"
32
36
," "
37
+ ," -- Perform some operation to the last list"
33
38
," app :: ([a] -> [a]) -> [[a]] -> [[a]]"
34
39
," app f [] = [f []]"
35
40
," app f [x] = [f x]"
36
41
," app f (x:xs) = [x] ++ app f xs"
37
42
," "
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"
40
54
," "
55
+ ," -- Escape a string"
41
56
," escape :: String -> String"
42
57
," escape s = [qot] ++ (s >>= escChar) ++ [qot]"
43
58
," where"
@@ -50,28 +65,39 @@ file =
50
65
," | otherwise = [chr]"
51
66
]]
52
67
68
+ -- Indent a line with 4 spaces
53
69
dent :: String -> String
54
70
dent = (" " ++ )
55
71
56
- rewrap :: [[String ]] -> [String ]
57
- rewrap = concat . pre (pre (" [" ++ )) . fil (pre (" ," ++ )) . app (app (++ " ]" ))
58
-
72
+ -- Perform some operation to the first list
59
73
pre :: ([a ] -> [a ]) -> [[a ]] -> [[a ]]
60
74
pre f [] = [f [] ]
61
75
pre f (x: xs) = [f x] ++ xs
62
76
77
+ -- Perform some operation to all but the first list
63
78
fil :: ([a ] -> [a ]) -> [[a ]] -> [[a ]]
64
79
fil _ [] = []
65
80
fil f (x: xs) = [x] ++ (f <$> xs)
66
81
82
+ -- Perform some operation to the last list
67
83
app :: ([a ] -> [a ]) -> [[a ]] -> [[a ]]
68
84
app f [] = [f [] ]
69
85
app f [x] = [f x]
70
86
app f (x: xs) = [x] ++ app f xs
71
87
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
74
99
100
+ -- Escape a string
75
101
escape :: String -> String
76
102
escape s = [qot] ++ (s >>= escChar) ++ [qot]
77
103
where
0 commit comments