In Haskell, I have defined a polymorphic data type Subst a
with a single constructor S :: [(String, a)] -> Subst a
as so:
data Subst a where
S :: [(String, a)] -> Subst a
deriving (Show)
I want to define a function get::String -> Subst a -> Maybe a
that takes a variable name and a substitution, and returns the value for which that variable must be substituted. If the substitution is not defined on the variable, the function should return Nothing.
I've tried the following:
get :: String -> Subst a -> Maybe a
get str (S[]) = Nothing
get str (S((a,b):xs)) = if str == a then Just b
else get str xs
But I'm getting errors. Any ideas why?
data Subst a = S [(String, a)] deriving Show
? – Tikhon JelvisMap String a
instead of[(String,a)]
. Lookups for lists take O(n) while lookups for maps take O(log n). – PetrMap a b
type has a very rich API for lookups and insertions. – Paul R