From 9a4cf15fc0e724e6bc93c6530b47ca45836da5ba Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 16 Mar 2020 16:27:39 +0100 Subject: variable and function context between prompts --- src/Assignment.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/Assignment.hs') diff --git a/src/Assignment.hs b/src/Assignment.hs index c086280..51e619a 100644 --- a/src/Assignment.hs +++ b/src/Assignment.hs @@ -5,6 +5,24 @@ import qualified Expr as E data Assignment = Variable String E.Expr | Function String String E.Expr + deriving (Eq) + +name :: Assignment -> String +name (Variable n _) = n +name (Function n _ _) = n + +type Context = [Assignment] + +update :: Context -> Assignment -> Context +update context a + | name a `elem` map name context = map replaceIf context + | otherwise = a:context + where replaceIf a' = if name a' == name a then a else a' + +get :: Context -> String -> Maybe Assignment +get context n = case found of [] -> Nothing + [a] -> Just a + where found = filter (\a -> name a == n) context instance Show Assignment where show (Variable name e) = name ++ " = " ++ show e -- cgit