Created
June 3, 2023 18:14
-
-
Save BRonen/dea29579939c19c603eadec6046752bd to your computer and use it in GitHub Desktop.
A key value data structure made in haskell just 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 | |
type Pair a = ( [Char], a ) | |
updateValue :: [Char] -> a -> [Pair a] -> [Pair a] | |
updateValue key value [] = [(key, value)] | |
updateValue key value [(a, b)] | |
| a == key = [(key, value)] | |
| otherwise = [(a, b), (key, value)] | |
updateValue key value ((a, b):xs) | |
| a == key = [(key, value)] ++ xs | |
| otherwise = [(a, b)] ++ updateValue key value xs | |
dropValue :: [Char] -> [Pair a] -> [Pair a] | |
dropValue _ [] = [] | |
dropValue key [(a, b)] = if a == key then [] else [(a, b)] | |
dropValue key ((a, b):xs) = if a == key then xs else [(a, b)] ++ dropValue key xs | |
insertPair :: [Char] -> a -> [Pair a] -> [Pair a] | |
insertPair key value pair = [(key, value)] ++ pair | |
getValueByKey :: [Char] -> [Pair a] -> Maybe a | |
getValueByKey _ [] = Nothing | |
getValueByKey key [(a, b)] | |
| a == key = Just b | |
| otherwise = Nothing | |
getValueByKey key ((a, b):xs) | |
| a == key = Just b | |
| otherwise = getValueByKey key xs | |
pair1 :: [Pair [Char]] | |
pair1 = [("hello", "World")] | |
main :: IO() | |
main = do | |
print(pair1) | |
print(insertPair "hello2" "2" pair1) | |
print(getValueByKey "hello" pair1) | |
print(getValueByKey "hello2" pair1) | |
print(updateValue "hello2" "1234" (updateValue "hello" "1234555" pair1)) | |
print(dropValue "hello" (updateValue "hello" "1234555" (updateValue "hello2" "1234555" pair1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment