Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
Models a bank account using a mutable record + a user exception:
type account = { mutable balance : int }
exception Insufficient
let withdraw acct amt =
if amt > acct.balance then raise Insufficient
else acct.balance <- acct.balance - amt
Sequence:
start 100
deposit 50 150
withdraw 30 120
withdraw 200 raises Insufficient
handler returns acct.balance (= 120, transaction rolled back)
Combines mutable record fields, user exception declaration,
try-with-bare-pattern, and verifies that a raise in the middle of a
sequence doesn't leave a partial mutation.
59 baseline programs total.
18 lines
358 B
OCaml
18 lines
358 B
OCaml
type account = { mutable balance : int }
|
|
|
|
exception Insufficient
|
|
|
|
let withdraw acct amt =
|
|
if amt > acct.balance then raise Insufficient
|
|
else acct.balance <- acct.balance - amt
|
|
|
|
let deposit acct amt = acct.balance <- acct.balance + amt
|
|
|
|
;;
|
|
|
|
let a = { balance = 100 } in
|
|
deposit a 50;
|
|
withdraw a 30;
|
|
try (withdraw a 200; -1)
|
|
with Insufficient -> a.balance
|