Reputation: 41
I have the following code:
type transaction = Withdraw of int | Deposit of int | Checkbalance
(* Bank account generator. *)
let make_account(opening_balance: int) =
let balance = ref opening_balance in
fun (t: transaction) ->
match t with
| Withdraw(m) -> if (!balance > m)
then
((balance := !balance - m);
(Printf.printf "Balance is %i" !balance))
else
print_string "Insufficient funds."
| Deposit(m) ->
((balance := !balance + m);
(Printf.printf "Balance is %i\n" !balance))
| Checkbalance -> (Printf.printf "Balance is %i\n" !balance)
;;
whenever I try to run the following command: make_account(100) Deposit(50) ;;
I get the following error: This function has type int -> transaction -> unit. It is applied to too many arguments; maybe you forgot a `;'.
However the following command works just fine Deposit(50) |> make_account(100) ;;
But why aren't these two line of codes equivalent? shouldn't (make_account 100) be replaced by (fun (t: transaction) -> ...) ? Because then I don't understand why my first attempt did not work.
Thank you!
Upvotes: 0
Views: 54
Reputation: 664395
Notice that Ocaml doesn't use parenthesis for function calls. In the statement
make_account (100) Deposit (50);;
you have two unnecessary grouping parenthesis, it is the same as
make_account 100 Deposit 50 ;;
where make_account
is applied with three arguments. What you meant to write was
make_account 100 (Deposit 50);;
which is equivalent to the parenthesis-less
Deposit 50 |> make_account 100;;
Upvotes: 3