You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"description": "Prints Hello, World! to the terminal.",
78
+
"author": "ACR1209",
79
+
"tags": [
80
+
"haskell",
81
+
"printing",
82
+
"hello-world",
83
+
"utility"
84
+
],
85
+
"contributors": [],
86
+
"code": "putStrLn \"Hello, World!\"\n"
87
+
}
88
+
]
89
+
},
90
+
{
91
+
"categoryName": "File Handling",
92
+
"snippets": [
93
+
{
94
+
"title": "Append to File",
95
+
"description": "Appends text to an existing file.",
96
+
"author": "ACR1209",
97
+
"tags": [
98
+
"haskell",
99
+
"file",
100
+
"append",
101
+
"utilty"
102
+
],
103
+
"contributors": [],
104
+
"code": "import System.IO\n\nappendToFile :: FilePath -> String -> IO ()\nappendToFile = appendFile \n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n let text = \"This will be appended to the file.\\n\"\n appendToFile file text\n"
105
+
},
106
+
{
107
+
"title": "Check if File Exists",
108
+
"description": "Checks if a file exists at a given path.",
109
+
"author": "ACR1209",
110
+
"tags": [
111
+
"haskell",
112
+
"file",
113
+
"exists"
114
+
],
115
+
"contributors": [],
116
+
"code": "import System.Directory (doesFileExist)\n\ncheckFileExists :: FilePath -> IO Bool\ncheckFileExists = doesFileExist\n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n exists <- checkFileExists file\n if exists then putStrLn \"File exists.\" else putStrLn \"File does not exist.\"\n"
117
+
},
118
+
{
119
+
"title": "Find Files in Directory by Type",
120
+
"description": "Finds all files in a directory with a specific extension.",
121
+
"author": "ACR1209",
122
+
"tags": [
123
+
"haskell",
124
+
"file",
125
+
"search",
126
+
"extension",
127
+
"filesystem"
128
+
],
129
+
"contributors": [],
130
+
"code": "import System.Directory (listDirectory)\nimport System.FilePath (takeExtension)\n\nfindFilesByExtension :: FilePath -> String -> IO [FilePath]\nfindFilesByExtension dir ext = do\n files <- listDirectory dir\n return $ filter (\\f -> takeExtension f == ext) files\n\nmain :: IO ()\nmain = do\n let directory = \".\"\n let ext = \".txt\"\n files <- findFilesByExtension directory ext\n mapM_ putStrLn files -- Output: list of txt files on the current directory\n"
131
+
},
132
+
{
133
+
"title": "Read File in Chunks",
134
+
"description": "Reads a file in chunks grouped by lines.",
135
+
"author": "ACR1209",
136
+
"tags": [
137
+
"haskell",
138
+
"file",
139
+
"read",
140
+
"chunks",
141
+
"utility"
142
+
],
143
+
"contributors": [],
144
+
"code": "import System.IO (openFile, IOMode(ReadMode), hGetContents)\nimport Data.List (unfoldr)\n\nreadFileInChunks :: FilePath -> Int -> IO [[String]]\nreadFileInChunks filePath chunkSize = do\n handle <- openFile filePath ReadMode\n contents <- hGetContents handle\n let linesList = lines contents\n return $ go linesList\n where\n go [] = []\n go xs = take chunkSize xs : go (drop chunkSize xs)\n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n let chunkSize = 3 -- Number of lines per chunk\n chunks <- readFileInChunks file chunkSize\n mapM_ (putStrLn . unlines) chunks\n\n"
145
+
},
146
+
{
147
+
"title": "Write to File",
148
+
"description": "Writes text to a file, overwriting any existing content.",
149
+
"author": "ACR1209",
150
+
"tags": [
151
+
"haskell",
152
+
"file",
153
+
"write"
154
+
],
155
+
"contributors": [],
156
+
"code": "import System.IO (writeFile)\n\nwriteToFile :: FilePath -> String -> IO ()\nwriteToFile = writeFile\n\nmain :: IO ()\nmain = do\n let file = \"example.txt\"\n let content = \"This is new content.\"\n writeToFile file content\n"
157
+
}
158
+
]
159
+
},
160
+
{
161
+
"categoryName": "Monads",
162
+
"snippets": [
163
+
{
164
+
"title": "Either Monad for Error Handling",
165
+
"description": "Using the Either monad to handle errors in a computation.",
166
+
"author": "ACR1209",
167
+
"tags": [
168
+
"haskell",
169
+
"monads",
170
+
"either",
171
+
"error handling"
172
+
],
173
+
"contributors": [],
174
+
"code": "safeDiv :: Int -> Int -> Either String Int\nsafeDiv _ 0 = Left \"Division by zero error\"\nsafeDiv x y = Right (x `div` y)\n\nmain :: IO ()\nmain = do\n let result = do\n a <- safeDiv 10 2\n b <- safeDiv a 0 -- This will trigger an error\n return b\n print result -- Output: Left \"Division by zero error\"\n"
175
+
},
176
+
{
177
+
"title": "Maybe Monad",
178
+
"description": "Using the Maybe monad to handle computations that might fail.",
179
+
"author": "ACR1209",
180
+
"tags": [
181
+
"haskell",
182
+
"monads",
183
+
"maybe"
184
+
],
185
+
"contributors": [],
186
+
"code": "safeDiv :: Int -> Int -> Maybe Int\nsafeDiv _ 0 = Nothing\nsafeDiv x y = Just (x `div` y)\n\nmain :: IO ()\nmain = do\n let result = do\n a <- safeDiv 10 2\n b <- safeDiv a 2\n return b\n print result -- Output: Just 2\n"
187
+
},
188
+
{
189
+
"title": "State Monad",
190
+
"description": "Managing mutable state using the State monad.",
191
+
"author": "ACR1209",
192
+
"tags": [
193
+
"haskell",
194
+
"monads",
195
+
"state",
196
+
"state-management"
197
+
],
198
+
"contributors": [],
199
+
"code": "import Control.Monad.State\n\nincrement :: State Int Int\nincrement = do\n count <- get\n put (count + 1)\n return count\n\nmain :: IO ()\nmain = do\n let (res1, intermediateState) = runState increment 0\n print res1 -- Output: 0\n let (result, finalState) = runState increment intermediateState\n print result -- Output: 1\n print finalState -- Output: 2\n\n"
200
+
},
201
+
{
202
+
"title": "Writer Monad",
203
+
"description": "Using the Writer monad to accumulate logs or other outputs alongside a computation.",
204
+
"author": "ACR1209",
205
+
"tags": [
206
+
"haskell",
207
+
"monads",
208
+
"writer",
209
+
"logs"
210
+
],
211
+
"contributors": [],
212
+
"code": "import Control.Monad.Writer\n\naddAndLog :: Int -> Int -> Writer [String] Int\naddAndLog x y = do\n tell [\"Adding \" ++ show x ++ \" and \" ++ show y]\n return (x + y)\n\nmain :: IO ()\nmain = do\n let (result, logs) = runWriter $ do\n res1 <- addAndLog 3 5\n addAndLog res1 1\n print result -- Output: 9\n print logs -- Output: [\"Adding 3 and 5\", \"Adding 8 and 1\"]\n"
213
+
}
214
+
]
215
+
},
216
+
{
217
+
"categoryName": "String Manipulation",
218
+
"snippets": [
219
+
{
220
+
"title": "Transform Camel Case to Snake Case",
221
+
"description": "Converts a Camel Case string to Snake case.",
0 commit comments