playground/haskell/Imp/Dict.hs

18 lines
344 B
Haskell

module Dict where
type Dict a = [(String, a)]
set :: Dict a -> String -> a -> Dict a
set d lbl val = case d of
[] -> [(lbl,val)]
(l,v):xs ->
if l==lbl then (l,val):xs
else (l,v):(set xs lbl val)
get :: Dict a -> String -> Maybe a
get d lbl = case d of
[] -> Nothing
(l,v):xs ->
if l==lbl then Just v
else get xs lbl