-
-
Notifications
You must be signed in to change notification settings - Fork 129
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 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 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!" | ||
``` |
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
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" | ||
``` |
18 changes: 18 additions & 0 deletions
snippets/haskell/string-manipulation/count-word-ocurrences.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,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)] | ||
``` |
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: 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
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: 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" | ||
``` |
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: 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." | ||
``` |
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.