Skip to content

Commit 819f66a

Browse files
committed
add haskell language and some basic categories/snippets
1 parent f1f3b6a commit 819f66a

14 files changed

+236
-0
lines changed

public/icons/haskell.svg

Lines changed: 6 additions & 0 deletions
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Binary Search
3+
description: Searches for an element in a sorted array using binary search.
4+
author: ACR1209
5+
tags: haskell,array,binary-search,search
6+
---
7+
8+
```hs
9+
binarySearch :: Ord a => a -> [a] -> Maybe Int
10+
binarySearch _ [] = Nothing
11+
binarySearch target xs = go 0 (length xs - 1)
12+
where
13+
go low high
14+
| low > high = Nothing
15+
| midElem < target = go (mid + 1) high
16+
| midElem > target = go low (mid - 1)
17+
| otherwise = Just mid
18+
where
19+
mid = (low + high) `div` 2
20+
midElem = xs !! mid
21+
22+
main :: IO ()
23+
main = do
24+
let array = [1, 2, 3, 4, 5]
25+
print $ binarySearch 3 array -- Output: Just 2
26+
print $ binarySearch 6 array -- Output: Nothing
27+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Chunk Array
3+
description: Splits an array into chunks of a specified size.
4+
author: ACR1209
5+
tags: haskell,array,chunk,utility
6+
---
7+
8+
```hs
9+
chunkArray :: Int -> [a] -> [[a]]
10+
chunkArray _ [] = []
11+
chunkArray n xs = take n xs : chunkArray n (drop n xs)
12+
13+
main :: IO ()
14+
main = do
15+
let array = [1, 2, 3, 4, 5, 6]
16+
print $ chunkArray 2 array -- Output: [[1, 2], [3, 4], [5, 6]]
17+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Flatten Array
3+
description: Flattens a multi-dimensional array.
4+
author: ACR1209
5+
tags: haskell,array,flatten,utility
6+
---
7+
8+
```hs
9+
flatten :: [[a]] -> [a]
10+
flatten = concat
11+
12+
main :: IO ()
13+
main = do
14+
let array = [[1, 2], [2], [3], [4]]
15+
print $ flatten array -- Output: [1, 2, 2, 3, 4]
16+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Matrix Transpose
3+
description: Transposes a 2D matrix.
4+
author: ACR1209
5+
tags: haskell,array,matrix,transpose
6+
---
7+
8+
```hs
9+
transposeMatrix :: [[a]] -> [[a]]
10+
transposeMatrix [] = []
11+
transposeMatrix ([]:_) = []
12+
transposeMatrix xs = map head xs : transposeMatrix (map tail xs)
13+
14+
main :: IO ()
15+
main = do
16+
let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
17+
print $ transposeMatrix matrix -- Output: [[1,4,7],[2,5,8],[3,6,9]]
18+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Remove duplicates
3+
description: Removes duplicate values from an array.
4+
author: ACR1209
5+
tags: haskell,array,deduplicate,utility
6+
---
7+
8+
```hs
9+
import Data.List (nub)
10+
11+
removeDuplicates :: Eq a => [a] -> [a]
12+
removeDuplicates = nub
13+
14+
-- Usage
15+
main :: IO ()
16+
main = do
17+
let array = [1, 2, 2, 3, 4, 4, 5]
18+
print $ removeDuplicates array -- Output: [1, 2, 3, 4, 5]
19+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: Hello, World!
3+
description: Prints Hello, World! to the terminal.
4+
author: ACR1209
5+
tags: haskell,printing,hello-world,utility
6+
---
7+
8+
```haskell
9+
putStrLn "Hello, World!"
10+
```

snippets/haskell/icon.svg

Lines changed: 6 additions & 0 deletions
Loading
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Transform Camel Case to Snake Case
3+
description: Converts a Camel Case string to Snake case.
4+
author: ACR1209
5+
tags: haskell,string,convert,camel-case,snake-case,utility
6+
---
7+
8+
```hs
9+
import Data.Char (isUpper, toLower)
10+
11+
camelToSnake :: String -> String
12+
camelToSnake [] = []
13+
camelToSnake (x:xs)
14+
| isUpper x = '_' : toLower x : camelToSnake xs
15+
| otherwise = x : camelToSnake xs
16+
17+
main :: IO ()
18+
main = do
19+
let camelCase = "camelCaseToSnakeCase"
20+
print $ camelToSnake camelCase -- Output: "camel_case_to_snake_case"
21+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Capitalize Words
3+
description: Capitalizes the first letter of each word in a string.
4+
author: ACR1209
5+
tags: haskell,string,capitalize,words
6+
---
7+
8+
```hs
9+
import Data.Char (toUpper)
10+
11+
capitalizeWords :: String -> String
12+
capitalizeWords = unwords . map capitalize . words
13+
where
14+
capitalize [] = []
15+
capitalize (x:xs) = toUpper x : xs
16+
17+
main :: IO ()
18+
main = do
19+
let sentence = "haskell is awesome"
20+
print $ capitalizeWords sentence -- Output: "Haskell Is Awesome"
21+
```

0 commit comments

Comments
 (0)