(* Object-oriented programming in SML *) exception AccessDenied exception InsufficientFunds (* Create an account with initial balance init and password pass. *) fun makeAccount init (pass : string) = let val bal = ref init val password = pass fun depos d = bal := !bal + d (* a "public" method *) fun balan () = !bal (* a "private" method *) fun withd w = if balan () >= w then (* a "private" method *) bal := !bal - w else raise InsufficientFunds in {deposit = depos, balance = fn p => if p = password then balan () else raise AccessDenied, withdraw = fn p => if p = password then withd else raise AccessDenied} end (* makeAccount : int -> string -> {balance:string -> int, deposit:int -> unit, withdraw:string -> int -> unit} Here are some examples of its use: - val acc = makeAccount 1000 "rosebud"; val acc = {balance=fn,deposit=fn,withdraw=fn} : {balance:string -> int, deposit:int -> unit, withdraw:string -> int -> unit} - #balance acc "rosebud"; val it = 1000 : int - #balance acc "kane"; uncaught exception AccessDenied raised at: account.sml:24.65-24.77 - #deposit acc 500; val it = () : unit - #withdraw acc "rosebud" 1200; val it = () : unit - #balance acc "rosebud"; val it = 300 : int - #withdraw acc "rosebud" 500; uncaught exception InsufficientFunds raised at: account.sml:20.15-20.32 *)