Skip to content

Instantly share code, notes, and snippets.

@eagletmt
Created February 17, 2010 12:26
Show Gist options
  • Save eagletmt/306554 to your computer and use it in GitHub Desktop.
Save eagletmt/306554 to your computer and use it in GitHub Desktop.
import Prelude hiding (lookup)
type Map k a = k -> Maybe a
empty :: Map k a
empty = const Nothing
insert :: Eq k => k -> a -> Map k a -> Map k a
insert k v f = \x -> if x == k then Just v else f x
delete :: Eq k => k -> Map k a -> Map k a
delete k f = \x -> if x == k then Nothing else f x
lookup :: Eq k => k -> Map k a -> Maybe a
lookup k f = f k
main = do
let t = insert 5 "foo" . insert 2 "bar" $ empty
print $ lookup 2 t
print $ lookup 0 t
let t' = delete 2 t
print $ lookup 2 t'
print $ lookup 5 t'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment