diff --git a/public/icons/haskell.svg b/public/icons/haskell.svg new file mode 100644 index 00000000..8163876f --- /dev/null +++ b/public/icons/haskell.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/snippets/haskell/array-manipulation/binary-search.md b/snippets/haskell/array-manipulation/binary-search.md new file mode 100644 index 00000000..24be7973 --- /dev/null +++ b/snippets/haskell/array-manipulation/binary-search.md @@ -0,0 +1,27 @@ +--- +title: Binary Search +description: Searches for an element in a sorted array using binary search. +author: ACR1209 +tags: haskell,array,binary-search,search +--- + +```hs +binarySearch :: Ord a => a -> [a] -> Maybe Int +binarySearch _ [] = Nothing +binarySearch target xs = go 0 (length xs - 1) + where + go low high + | low > high = Nothing + | midElem < target = go (mid + 1) high + | midElem > target = go low (mid - 1) + | otherwise = Just mid + where + mid = (low + high) `div` 2 + midElem = xs !! mid + +main :: IO () +main = do + let array = [1, 2, 3, 4, 5] + print $ binarySearch 3 array -- Output: Just 2 + print $ binarySearch 6 array -- Output: Nothing +``` \ No newline at end of file diff --git a/snippets/haskell/array-manipulation/chunk-array.md b/snippets/haskell/array-manipulation/chunk-array.md new file mode 100644 index 00000000..6d85256e --- /dev/null +++ b/snippets/haskell/array-manipulation/chunk-array.md @@ -0,0 +1,17 @@ +--- +title: Chunk Array +description: Splits an array into chunks of a specified size. +author: ACR1209 +tags: haskell,array,chunk,utility +--- + +```hs +chunkArray :: Int -> [a] -> [[a]] +chunkArray _ [] = [] +chunkArray n xs = take n xs : chunkArray n (drop n xs) + +main :: IO () +main = do + let array = [1, 2, 3, 4, 5, 6] + print $ chunkArray 2 array -- Output: [[1, 2], [3, 4], [5, 6]] +``` \ No newline at end of file diff --git a/snippets/haskell/array-manipulation/flatten-array.md b/snippets/haskell/array-manipulation/flatten-array.md new file mode 100644 index 00000000..9be1cb27 --- /dev/null +++ b/snippets/haskell/array-manipulation/flatten-array.md @@ -0,0 +1,16 @@ +--- +title: Flatten Array +description: Flattens a multi-dimensional array. +author: ACR1209 +tags: haskell,array,flatten,utility +--- + +```hs +flatten :: [[a]] -> [a] +flatten = concat + +main :: IO () +main = do + let array = [[1, 2], [2], [3], [4]] + print $ flatten array -- Output: [1, 2, 2, 3, 4] +``` diff --git a/snippets/haskell/array-manipulation/matrix-transpose.md b/snippets/haskell/array-manipulation/matrix-transpose.md new file mode 100644 index 00000000..8bec051d --- /dev/null +++ b/snippets/haskell/array-manipulation/matrix-transpose.md @@ -0,0 +1,18 @@ +--- +title: Matrix Transpose +description: Transposes a 2D matrix. +author: ACR1209 +tags: haskell,array,matrix,transpose +--- + +```hs +transposeMatrix :: [[a]] -> [[a]] +transposeMatrix [] = [] +transposeMatrix ([]:_) = [] +transposeMatrix xs = map head xs : transposeMatrix (map tail xs) + +main :: IO () +main = do + let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] + print $ transposeMatrix matrix -- Output: [[1,4,7],[2,5,8],[3,6,9]] +``` \ No newline at end of file diff --git a/snippets/haskell/array-manipulation/remove-duplicates.md b/snippets/haskell/array-manipulation/remove-duplicates.md new file mode 100644 index 00000000..3af42fa8 --- /dev/null +++ b/snippets/haskell/array-manipulation/remove-duplicates.md @@ -0,0 +1,19 @@ +--- +title: Remove duplicates +description: Removes duplicate values from an array. +author: ACR1209 +tags: haskell,array,deduplicate,utility +--- + +```hs +import Data.List (nub) + +removeDuplicates :: Eq a => [a] -> [a] +removeDuplicates = nub + +-- Usage +main :: IO () +main = do + let array = [1, 2, 2, 3, 4, 4, 5] + print $ removeDuplicates array -- Output: [1, 2, 3, 4, 5] +``` diff --git a/snippets/haskell/basics/hello-world.md b/snippets/haskell/basics/hello-world.md new file mode 100644 index 00000000..a1da8874 --- /dev/null +++ b/snippets/haskell/basics/hello-world.md @@ -0,0 +1,10 @@ +--- +title: Hello, World! +description: Prints Hello, World! to the terminal. +author: ACR1209 +tags: haskell,printing,hello-world,utility +--- + +```haskell +putStrLn "Hello, World!" +``` diff --git a/snippets/haskell/file-handling/append-to-file.md b/snippets/haskell/file-handling/append-to-file.md new file mode 100644 index 00000000..ecdef843 --- /dev/null +++ b/snippets/haskell/file-handling/append-to-file.md @@ -0,0 +1,19 @@ +--- +title: Append to File +description: Appends text to an existing file. +author: ACR1209 +tags: haskell,file,append,utilty +--- + +```hs +import System.IO + +appendToFile :: FilePath -> String -> IO () +appendToFile = appendFile + +main :: IO () +main = do + let file = "example.txt" + let text = "This will be appended to the file.\n" + appendToFile file text +``` \ No newline at end of file diff --git a/snippets/haskell/file-handling/file-exists.md b/snippets/haskell/file-handling/file-exists.md new file mode 100644 index 00000000..247761db --- /dev/null +++ b/snippets/haskell/file-handling/file-exists.md @@ -0,0 +1,19 @@ +--- +title: Check if File Exists +description: Checks if a file exists at a given path. +author: ACR1209 +tags: haskell,file,exists +--- + +```hs +import System.Directory (doesFileExist) + +checkFileExists :: FilePath -> IO Bool +checkFileExists = doesFileExist + +main :: IO () +main = do + let file = "example.txt" + exists <- checkFileExists file + if exists then putStrLn "File exists." else putStrLn "File does not exist." +``` \ No newline at end of file diff --git a/snippets/haskell/file-handling/find-files-with-extension-in-directory.md b/snippets/haskell/file-handling/find-files-with-extension-in-directory.md new file mode 100644 index 00000000..e9f4f9ce --- /dev/null +++ b/snippets/haskell/file-handling/find-files-with-extension-in-directory.md @@ -0,0 +1,23 @@ +--- +title: Find Files in Directory by Type +description: Finds all files in a directory with a specific extension. +author: ACR1209 +tags: haskell,file,search,extension,filesystem +--- + +```hs +import System.Directory (listDirectory) +import System.FilePath (takeExtension) + +findFilesByExtension :: FilePath -> String -> IO [FilePath] +findFilesByExtension dir ext = do + files <- listDirectory dir + return $ filter (\f -> takeExtension f == ext) files + +main :: IO () +main = do + let directory = "." + let ext = ".txt" + files <- findFilesByExtension directory ext + mapM_ putStrLn files -- Output: list of txt files on the current directory +``` \ No newline at end of file diff --git a/snippets/haskell/file-handling/read-chunks.md b/snippets/haskell/file-handling/read-chunks.md new file mode 100644 index 00000000..d6844bd6 --- /dev/null +++ b/snippets/haskell/file-handling/read-chunks.md @@ -0,0 +1,29 @@ +--- +title: Read File in Chunks +description: Reads a file in chunks grouped by lines. +author: ACR1209 +tags: haskell,file,read,chunks,utility +--- + +```hs +import System.IO (openFile, IOMode(ReadMode), hGetContents) +import Data.List (unfoldr) + +readFileInChunks :: FilePath -> Int -> IO [[String]] +readFileInChunks filePath chunkSize = do + handle <- openFile filePath ReadMode + contents <- hGetContents handle + let linesList = lines contents + return $ go linesList + where + go [] = [] + go xs = take chunkSize xs : go (drop chunkSize xs) + +main :: IO () +main = do + let file = "example.txt" + let chunkSize = 3 -- Number of lines per chunk + chunks <- readFileInChunks file chunkSize + mapM_ (putStrLn . unlines) chunks + +``` \ No newline at end of file diff --git a/snippets/haskell/file-handling/write-to-file.md b/snippets/haskell/file-handling/write-to-file.md new file mode 100644 index 00000000..33fed448 --- /dev/null +++ b/snippets/haskell/file-handling/write-to-file.md @@ -0,0 +1,19 @@ +--- +title: Write to File +description: Writes text to a file, overwriting any existing content. +author: ACR1209 +tags: haskell,file,write +--- + +```hs +import System.IO (writeFile) + +writeToFile :: FilePath -> String -> IO () +writeToFile = writeFile + +main :: IO () +main = do + let file = "example.txt" + let content = "This is new content." + writeToFile file content +``` \ No newline at end of file diff --git a/snippets/haskell/icon.svg b/snippets/haskell/icon.svg new file mode 100644 index 00000000..8163876f --- /dev/null +++ b/snippets/haskell/icon.svg @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/snippets/haskell/monads/either-monad.md b/snippets/haskell/monads/either-monad.md new file mode 100644 index 00000000..883a4c50 --- /dev/null +++ b/snippets/haskell/monads/either-monad.md @@ -0,0 +1,20 @@ +--- +title: Either Monad for Error Handling +description: Using the Either monad to handle errors in a computation. +author: ACR1209 +tags: haskell, monads, either, error handling +--- + +```hs +safeDiv :: Int -> Int -> Either String Int +safeDiv _ 0 = Left "Division by zero error" +safeDiv x y = Right (x `div` y) + +main :: IO () +main = do + let result = do + a <- safeDiv 10 2 + b <- safeDiv a 0 -- This will trigger an error + return b + print result -- Output: Left "Division by zero error" +``` \ No newline at end of file diff --git a/snippets/haskell/monads/maybe-monad.md b/snippets/haskell/monads/maybe-monad.md new file mode 100644 index 00000000..4c33f856 --- /dev/null +++ b/snippets/haskell/monads/maybe-monad.md @@ -0,0 +1,20 @@ +--- +title: Maybe Monad +description: Using the Maybe monad to handle computations that might fail. +author: ACR1209 +tags: haskell, monads, maybe +--- + +```hs +safeDiv :: Int -> Int -> Maybe Int +safeDiv _ 0 = Nothing +safeDiv x y = Just (x `div` y) + +main :: IO () +main = do + let result = do + a <- safeDiv 10 2 + b <- safeDiv a 2 + return b + print result -- Output: Just 2 +``` \ No newline at end of file diff --git a/snippets/haskell/monads/state-monad.md b/snippets/haskell/monads/state-monad.md new file mode 100644 index 00000000..a723bb55 --- /dev/null +++ b/snippets/haskell/monads/state-monad.md @@ -0,0 +1,25 @@ +--- +title: State Monad +description: Managing mutable state using the State monad. +author: ACR1209 +tags: haskell, monads, state, state-management +--- + +```hs +import Control.Monad.State + +increment :: State Int Int +increment = do + count <- get + put (count + 1) + return count + +main :: IO () +main = do + let (res1, intermediateState) = runState increment 0 + print res1 -- Output: 0 + let (result, finalState) = runState increment intermediateState + print result -- Output: 1 + print finalState -- Output: 2 + +``` \ No newline at end of file diff --git a/snippets/haskell/monads/writer-monad.md b/snippets/haskell/monads/writer-monad.md new file mode 100644 index 00000000..f1b3ac97 --- /dev/null +++ b/snippets/haskell/monads/writer-monad.md @@ -0,0 +1,23 @@ +--- +title: Writer Monad +description: Using the Writer monad to accumulate logs or other outputs alongside a computation. +author: ACR1209 +tags: haskell, monads, writer, logs +--- + +```hs +import Control.Monad.Writer + +addAndLog :: Int -> Int -> Writer [String] Int +addAndLog x y = do + tell ["Adding " ++ show x ++ " and " ++ show y] + return (x + y) + +main :: IO () +main = do + let (result, logs) = runWriter $ do + res1 <- addAndLog 3 5 + addAndLog res1 1 + print result -- Output: 9 + print logs -- Output: ["Adding 3 and 5", "Adding 8 and 1"] +``` \ No newline at end of file diff --git a/snippets/haskell/string-manipulation/camelcase-to-snakecase.md b/snippets/haskell/string-manipulation/camelcase-to-snakecase.md new file mode 100644 index 00000000..c3c09a14 --- /dev/null +++ b/snippets/haskell/string-manipulation/camelcase-to-snakecase.md @@ -0,0 +1,21 @@ +--- +title: Transform Camel Case to Snake Case +description: Converts a Camel Case string to Snake case. +author: ACR1209 +tags: haskell,string,convert,camel-case,snake-case,utility +--- + +```hs +import Data.Char (isUpper, toLower) + +camelToSnake :: String -> String +camelToSnake [] = [] +camelToSnake (x:xs) + | isUpper x = '_' : toLower x : camelToSnake xs + | otherwise = x : camelToSnake xs + +main :: IO () +main = do + let camelCase = "camelCaseToSnakeCase" + print $ camelToSnake camelCase -- Output: "camel_case_to_snake_case" +``` \ No newline at end of file diff --git a/snippets/haskell/string-manipulation/capitalize-words.md b/snippets/haskell/string-manipulation/capitalize-words.md new file mode 100644 index 00000000..dbf75c61 --- /dev/null +++ b/snippets/haskell/string-manipulation/capitalize-words.md @@ -0,0 +1,21 @@ +--- +title: Capitalize Words +description: Capitalizes the first letter of each word in a string. +author: ACR1209 +tags: haskell,string,capitalize,words +--- + +```hs +import Data.Char (toUpper) + +capitalizeWords :: String -> String +capitalizeWords = unwords . map capitalize . words + where + capitalize [] = [] + capitalize (x:xs) = toUpper x : xs + +main :: IO () +main = do + let sentence = "haskell is awesome" + print $ capitalizeWords sentence -- Output: "Haskell Is Awesome" +``` \ No newline at end of file diff --git a/snippets/haskell/string-manipulation/count-word-ocurrences.md b/snippets/haskell/string-manipulation/count-word-ocurrences.md new file mode 100644 index 00000000..6c677631 --- /dev/null +++ b/snippets/haskell/string-manipulation/count-word-ocurrences.md @@ -0,0 +1,18 @@ +--- +title: Count Word Occurrences in String +description: Counts the occurrences of each word in a given string. +author: ACR1209 +tags: haskell,string,occurrences,word-count +--- + +```hs +import Data.List (group, sort) + +countWordOccurrences :: String -> [(String, Int)] +countWordOccurrences = map (\(w:ws) -> (w, length (w:ws))) . group . sort . words + +main :: IO () +main = do + let text = "haskell is awesome and haskell is fun" + print $ countWordOccurrences text -- Output: [("and",1),("awesome",1),("fun",1),("haskell",2),("is",2)] +``` \ No newline at end of file diff --git a/snippets/haskell/string-manipulation/remove-punctuation.md b/snippets/haskell/string-manipulation/remove-punctuation.md new file mode 100644 index 00000000..64bc18be --- /dev/null +++ b/snippets/haskell/string-manipulation/remove-punctuation.md @@ -0,0 +1,18 @@ +--- +title: Remove Punctuation +description: Removes all punctuation from a given string. +author: ACR1209 +tags: haskell,string,punctuation,remove +--- + +```hs +import Data.Char (isPunctuation) + +removePunctuation :: String -> String +removePunctuation = filter (not . isPunctuation) + +main :: IO () +main = do + let text = "Hello, Haskell! How's it going?" + print $ removePunctuation text -- Output: "Hello Haskell Hows it going" +``` \ No newline at end of file diff --git a/snippets/haskell/string-manipulation/snakecase-to-camelcase.md b/snippets/haskell/string-manipulation/snakecase-to-camelcase.md new file mode 100644 index 00000000..14664088 --- /dev/null +++ b/snippets/haskell/string-manipulation/snakecase-to-camelcase.md @@ -0,0 +1,20 @@ +--- +title: Transform from Snake Case to Camel Case +description: Converts a Snake Case string to Camel Case. +author: ACR1209 +tags: haskell,string,convert,snake-case,camel-case,utilty +--- + +```hs +import Data.Char (toUpper) + +snakeToCamel :: String -> String +snakeToCamel [] = [] +snakeToCamel ('_':x:xs) = toUpper x : snakeToCamel xs +snakeToCamel (x:xs) = x : snakeToCamel xs + +main :: IO () +main = do + let snakeCase = "snake_case_to_camel_case" + print $ snakeToCamel snakeCase -- Output: "snakeCaseToCamelCase" +``` \ No newline at end of file diff --git a/snippets/haskell/string-manipulation/truncate-string.md b/snippets/haskell/string-manipulation/truncate-string.md new file mode 100644 index 00000000..fe687d3c --- /dev/null +++ b/snippets/haskell/string-manipulation/truncate-string.md @@ -0,0 +1,19 @@ +--- +title: Truncate Strings +description: Truncates a string to a specified length, optionally adding an ellipsis. +author: ACR1209 +tags: haskell,string,truncate,utility +--- + +```hs +truncateString :: Int -> String -> String +truncateString maxLength str + | length str <= maxLength = str + | otherwise = take (maxLength - 3) str ++ "..." + +main :: IO () +main = do + let longString = "Haskell is a powerful functional programming language." + print $ truncateString 20 longString -- Output: "Haskell is a powe..." + print $ truncateString 54 longString -- Output: "Haskell is a powerful functional programming language." +``` \ No newline at end of file