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 1 commit
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
Next Next commit
add haskell language and some basic categories/snippets
  • Loading branch information
ACR1209 committed Jan 2, 2025
commit 819f66ac79d4538ba967ab3a82adf6b896b51111
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!"
```
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.
[フレーム]
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"
```
18 changes: 18 additions & 0 deletions snippets/haskell/string-manipulation/count-word-ocurrences.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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)]
```
18 changes: 18 additions & 0 deletions snippets/haskell/string-manipulation/remove-punctuation.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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"
```
20 changes: 20 additions & 0 deletions snippets/haskell/string-manipulation/snakecase-to-camelcase.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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"
```
19 changes: 19 additions & 0 deletions snippets/haskell/string-manipulation/truncate-string.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -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."
```

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