pattern matching - How can I assign a reference cell in a function and then return the value in sml -
so have function in sml takes integer value , refs it. how can assign new value variable balance , return balance? in deposit , withdraw patterns?
datatype message = getbalance | deposit of int | withdraw of int; fun opening_account init_amt = let val balance = ref init_amt in fn getbalance => !balance | deposit x => balance = !balance + x | withdraw x => balance = !balance - x end;
to update reference cell, use :=
. i.e.:
balance := !balance + x
if wish both update balance, , return new value, 1 after other ;
:
(balance := !balance + x; !balance)
Comments
Post a Comment