Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
Mathys-Gasnier merged 10 commits into quicksnip-dev:main from ACR1209:haskell-snippets
Jan 2, 2025
Merged
Show file tree
Hide file tree
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 Jan 2, 2025
331371e
added haskell append to file snippet
ACR1209 Jan 2, 2025
123c169
add Haskell snippet to check if a file exists
ACR1209 Jan 2, 2025
f6465f4
add Haskell snippet to find files in a directory by extension
ACR1209 Jan 2, 2025
db68080
add Haskell snippet to read a file in chunks by lines
ACR1209 Jan 2, 2025
c92a041
add Haskell snippet to write text to a file
ACR1209 Jan 2, 2025
2c31172
add Haskell monad category and snippet of using the Maybe monad
ACR1209 Jan 2, 2025
63ad862
add Haskell snippet for Either monad to handle errors in computations
ACR1209 Jan 2, 2025
f2ba64a
add Haskell snippet for Writer monad to accumulate logs alongside com...
ACR1209 Jan 2, 2025
7920cd5
add Haskell snippet for State monad to manage mutable state
ACR1209 Jan 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions public/icons/haskell.svg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
27 changes: 27 additions & 0 deletions snippets/haskell/array-manipulation/binary-search.md
View file Open in desktop
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
```
17 changes: 17 additions & 0 deletions snippets/haskell/array-manipulation/chunk-array.md
View file Open in desktop
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]]
```
16 changes: 16 additions & 0 deletions snippets/haskell/array-manipulation/flatten-array.md
View file Open in desktop
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]
```
18 changes: 18 additions & 0 deletions snippets/haskell/array-manipulation/matrix-transpose.md
View file Open in desktop
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]]
```
19 changes: 19 additions & 0 deletions snippets/haskell/array-manipulation/remove-duplicates.md
View file Open in desktop
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]
```
10 changes: 10 additions & 0 deletions snippets/haskell/basics/hello-world.md
View file Open in desktop
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!"
```
19 changes: 19 additions & 0 deletions snippets/haskell/file-handling/append-to-file.md
View file Open in desktop
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
```
19 changes: 19 additions & 0 deletions snippets/haskell/file-handling/file-exists.md
View file Open in desktop
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."
```
View file Open in desktop
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
```
29 changes: 29 additions & 0 deletions snippets/haskell/file-handling/read-chunks.md
View file Open in desktop
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

```
19 changes: 19 additions & 0 deletions snippets/haskell/file-handling/write-to-file.md
View file Open in desktop
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
```
6 changes: 6 additions & 0 deletions snippets/haskell/icon.svg
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
20 changes: 20 additions & 0 deletions snippets/haskell/monads/either-monad.md
View file Open in desktop
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"
```
20 changes: 20 additions & 0 deletions snippets/haskell/monads/maybe-monad.md
View file Open in desktop
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
```
25 changes: 25 additions & 0 deletions snippets/haskell/monads/state-monad.md
View file Open in desktop
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

```
23 changes: 23 additions & 0 deletions snippets/haskell/monads/writer-monad.md
View file Open in desktop
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
View file Open in desktop
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"
```
21 changes: 21 additions & 0 deletions snippets/haskell/string-manipulation/capitalize-words.md
View file Open in desktop
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"
```
Loading
Loading

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /