【发布时间】:2020-03-25 18:39:03
【问题描述】:
我有以下代码:
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)
;;
每当我尝试运行以下命令时:make_account(100) Deposit(50) ;; 我收到以下错误:此函数的类型为 int -> transaction -> unit。它适用于太多的论点;也许你忘记了一个 `;'。
但是以下命令可以正常工作Deposit(50) |> make_account(100) ;;
但是为什么这两行代码不等效呢? (make_account 100) 不应该替换为 (fun (t: transaction) -> ...) 吗?因为那时我不明白为什么我的第一次尝试没有奏效。
谢谢!
【问题讨论】:
标签: ocaml