CS计算机代考程序代写 Haskell — CPSC 312 – 2019 – Functional Dictionaries in Haskell

— CPSC 312 – 2019 – Functional Dictionaries in Haskell

— To run it, try:
— ghci
— :load FunDict

module FunDict
(Dict,
emptyDict, — Dict k v
getval, — (Ord k) => k -> Dict k v -> v
insertval, — (Ord k) => k -> v -> Dict k v -> Dict k v
stats, — Dict k v -> [Char]
show
) where

newtype Dict k v = FunDict (k -> Maybe v)

emptyDict:: Dict k v
emptyDict = FunDict ( \key -> Nothing)

getval :: (Eq k) => k -> Dict k v -> Maybe v
getval key (FunDict f) = f key

insertval :: (Eq k) => k -> v -> Dict k v -> Dict k v
insertval key val (FunDict f)
= FunDict (\k -> if k==key then Just val else f k)

stats di = “Stats not available”

instance Show (Dict k t) where
show d = “Function_dictionary”