I am trying the simulate a save functionality for the value of a given variables. Initially I create an empty space and then when I want to add a value of a variable, I do an update of this variable in this save space.
I want to be able to have the possibility of multiple save space, and with one to have its own variable values.
I have the following code
type Variable = String
type Val = Int
type Store = Variable -> Val
init :: Store
init = (\x -> 0)
fetch :: Store -> Variable ->Val
fetch store variable = store variable
update :: Store -> Variable -> Val -> Store
update s v val = (\x -> if x == v then val else init v)
And the execution i make is this:
> Main> s1 = init
> *Main> s2 = update s1 "x" 10
> *Main> s2 = update s2 "y" 30
> *Main> fetch s2 "x"
0
> *Main> fetch s2 "y"
30
> *Main>
So the problem here is that the function update does not "saves" all the variables values, just the last one.
A correct excution would be this:
> Main> s1 = init
> *Main> s2 = update s1 "x" 10
> *Main> s2 = update s2 "y" 30
> *Main> s2 = update s2 "z" 50
> *Main> fetch s2 "x"
10
> *Main> fetch s2 "y"
30
> *Main> fetch s2 "z"
50
> *Main> fetch s2 "w"
0