[ create a new paste ] login | about

Link: http://codepad.org/V821J8TN    [ raw code | output | fork ]

Haskell, pasted on Oct 14:
module Main where

import Prelude hiding (lookup)

class MapLike m where
    empty :: m k v
    lookup :: Ord k => k -> m k v -> Maybe v
    insert :: Ord k => k -> v -> m k v -> m k v
    delete :: Ord k => k -> m k v -> m k v
    fromList :: Ord k => [(k,v)] -> m k v

newtype ArrowMap k v = ArrowMap { getArrowMap :: k -> Maybe v }

instance MapLike ArrowMap where
    empty = ArrowMap $ const Nothing
    lookup k (ArrowMap f) = f k
    insert k v (ArrowMap f) = ArrowMap (\k' -> if k == k' then Just v else f k)
    delete k (ArrowMap f) = ArrowMap (\k' -> if k == k' then Nothing else f k)
    fromList [] = empty
    fromList ((k,v):xs) = insert k v (fromList xs)


main :: IO()
main = do
  print $ lookup 2 kv
  print $ lookup 1 kv
    where
      kv :: ArrowMap Int String
      kv = fromList [(2,"Hello"), (1, "Hola")]


Output:
1
2
Just "Hello"
Nothing


Create a new paste based on this one


Comments: