-
-
Notifications
You must be signed in to change notification settings - Fork 130
Add Haskell snippets #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add Haskell snippets #109
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
819f66a
add haskell language and some basic categories/snippets
ACR1209 331371e
added haskell append to file snippet
ACR1209 123c169
add Haskell snippet to check if a file exists
ACR1209 f6465f4
add Haskell snippet to find files in a directory by extension
ACR1209 db68080
add Haskell snippet to read a file in chunks by lines
ACR1209 c92a041
add Haskell snippet to write text to a file
ACR1209 2c31172
add Haskell monad category and snippet of using the Maybe monad
ACR1209 63ad862
add Haskell snippet for Either monad to handle errors in computations
ACR1209 f2ba64a
add Haskell snippet for Writer monad to accumulate logs alongside com...
ACR1209 7920cd5
add Haskell snippet for State monad to manage mutable state
ACR1209 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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." | ||
``` |
23 changes: 23 additions & 0 deletions
snippets/haskell/file-handling/find-files-with-extension-in-directory.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[γγ¬γΌγ ]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] | ||
``` |
21 changes: 21 additions & 0 deletions
snippets/haskell/string-manipulation/camelcase-to-snakecase.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.