Skip to content

Commit 12d2ae6

Browse files
Merge pull request #109 from ACR1209/haskell-snippets
Add Haskell snippets
2 parents 11255aa + 7920cd5 commit 12d2ae6

23 files changed

+433
-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+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Append to File
3+
description: Appends text to an existing file.
4+
author: ACR1209
5+
tags: haskell,file,append,utilty
6+
---
7+
8+
```hs
9+
import System.IO
10+
11+
appendToFile :: FilePath -> String -> IO ()
12+
appendToFile = appendFile
13+
14+
main :: IO ()
15+
main = do
16+
let file = "example.txt"
17+
let text = "This will be appended to the file.\n"
18+
appendToFile file text
19+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Check if File Exists
3+
description: Checks if a file exists at a given path.
4+
author: ACR1209
5+
tags: haskell,file,exists
6+
---
7+
8+
```hs
9+
import System.Directory (doesFileExist)
10+
11+
checkFileExists :: FilePath -> IO Bool
12+
checkFileExists = doesFileExist
13+
14+
main :: IO ()
15+
main = do
16+
let file = "example.txt"
17+
exists <- checkFileExists file
18+
if exists then putStrLn "File exists." else putStrLn "File does not exist."
19+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: Find Files in Directory by Type
3+
description: Finds all files in a directory with a specific extension.
4+
author: ACR1209
5+
tags: haskell,file,search,extension,filesystem
6+
---
7+
8+
```hs
9+
import System.Directory (listDirectory)
10+
import System.FilePath (takeExtension)
11+
12+
findFilesByExtension :: FilePath -> String -> IO [FilePath]
13+
findFilesByExtension dir ext = do
14+
files <- listDirectory dir
15+
return $ filter (\f -> takeExtension f == ext) files
16+
17+
main :: IO ()
18+
main = do
19+
let directory = "."
20+
let ext = ".txt"
21+
files <- findFilesByExtension directory ext
22+
mapM_ putStrLn files -- Output: list of txt files on the current directory
23+
```

0 commit comments

Comments
 (0)