tags:

views:

30

answers:

1

I'm working on making functions that'll perform different functions that a bank would need.

I'm given this:

signature ACCOUNT =
sig
  type account
  val open : int -> account
  val trans : account * int -> account option
  val balance : account -> int
  val interest : account -> account
end;

I now need to declare some functions:

open n which'll open an account with the start balance n, where if n<0 it'll return Domain
trans (k,n) will "transfer" or add n to the account k (or subtract it if n<0) - if the transaction will result in a negative balance it returns NONE
balance k returns the balance of the account k
interest k adds interest to the account k

This is what I have:

structure Testaccount : ACCOUNT =
struct
type account = int
  fun open n        = if n < 0 then raise Domain else n
  fun trans (k , n) = if k + n < 0 then NONE else SOME(k + n)
  fun balance k     = k

But I'm confused about the interest function.
I need to declare the function so that at every addition of interest, 4% of the balance will be added to the account (the balance is from right after the latest addition of interest, unless money has been removed from the account in which case 0 will be added to the account). If it's the first time interest is added the n from open is used as basis for the balance.

I hope I'm making sense here because I'm having a hard time figuring out how to do this. I need to somehow be able to keep track of the current balance, the balance after the last addition of interest and if money has been removed from the account.

Any ideas as to how I can do this?

A: 

Maybe one could change the account type to int list, to save all transactions on the account, and then use member to determine whether the interests should be added or not.

Hunteren
GeorgeWChubbySo what did you end up doing?
Hunteren