Skip to content

Instantly share code, notes, and snippets.

@skalahonza
Created May 23, 2018 11:32
Show Gist options
  • Save skalahonza/2e742ce36eed7d1a6097649add5cfc45 to your computer and use it in GitHub Desktop.
Save skalahonza/2e742ce36eed7d1a6097649add5cfc45 to your computer and use it in GitHub Desktop.
Map implementation in Haskell (trivia stupid and slow)
type KeyVal a b = (a, b)
type Map a b = [KeyVal a b]
test = initmap ["a","b","c"] [1,2,3]
myZip (k:ks) (v:vs) = [(k,v)] ++ myZip ks vs
myZip [] [] = []
get_key (a, b) = a
get_val (a, b) = b
initmap :: [a] -> [b] -> (Map a b)
initmap [] [] = []
initmap keys values = (myZip keys values)
find:: (Eq a) => (Map a b) -> a -> b -> b
find [] key def = def
find (m:ms) key def | (get_key m) == key = (get_val m)
| otherwise = (find ms key def)
delete :: (Eq a) => (Map a b) -> a -> (Map a b)
delete map key = [y | y <- map, (get_key y) /= key]
add :: (Eq a) => (Map a b) -> a -> b -> (Map a b)
add map key value = ([(key,value)] ++ (delete map key))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment