Created
June 4, 2023 06:14
-
-
Save BRonen/039f0838492e94bf9a680218a00c7d2d to your computer and use it in GitHub Desktop.
An example of linked list made in haskell for fun
This file contains 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
module Main where | |
data List a = End a | Node a (List a) deriving (Show) | |
prepend :: a -> List a -> List a | |
prepend value x = Node value x | |
append :: a -> List a -> List a | |
append value (End x) = Node x (End value) | |
append value (Node x y) = Node x (append value y) | |
insertAt :: a -> Int -> List a -> List a | |
insertAt value i list@(End x) | |
| i == 0 = Node value list | |
| otherwise = Node x (End value) | |
insertAt value i list@(Node x y) | |
| i == 0 = Node value list | |
| otherwise = Node x (insertAt value (i - 1) y) | |
dropAt :: Int -> List a -> List a | |
dropAt i list@(End x) | |
| i == 0 = list | |
| otherwise = list | |
dropAt i list@(Node x y) | |
| i == 0 = y | |
| otherwise = case y of | |
End z -> if i == 1 then End x else Node x y | |
_ -> Node x (dropAt (i - 1) y) | |
revert :: List a -> List a | |
revert (End x) = End x | |
revert (Node x (End y)) = Node y (End x) | |
revert (Node x y) = append x (revert y) | |
main = do | |
print $ (dropAt 0 $ insertAt 10 3 $ prepend 5 $ append 2 $ append 3 $ append 6 $ End 4) | |
print $ (revert $ dropAt 0 $ insertAt 10 3 $ prepend 5 $ append 2 $ append 3 $ append 6 $ End 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment