ocaml: phase 5.1 atm.ml baseline (mutable record + exception + try/with, balance = 120)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 34s
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.
This commit is contained in:
17
lib/ocaml/baseline/atm.ml
Normal file
17
lib/ocaml/baseline/atm.ml
Normal file
@@ -0,0 +1,17 @@
|
||||
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
|
||||
@@ -2,6 +2,7 @@
|
||||
"ackermann.ml": 125,
|
||||
"anagram_check.ml": 2,
|
||||
"anagrams.ml": 3,
|
||||
"atm.ml": 120,
|
||||
"bag.ml": 3,
|
||||
"bf_full.ml": 6,
|
||||
"bigint_add.ml": 28,
|
||||
|
||||
@@ -407,6 +407,15 @@ _Newest first._
|
||||
binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree *
|
||||
'a tree`) with insert + in-order traversal. Tests parametric ADT,
|
||||
recursive match, List.append, List.fold_left.
|
||||
- 2026-05-09 Phase 5.1 — atm.ml baseline (mutable record + custom
|
||||
exception + try/with, balance 120 after rollback). Models a bank
|
||||
account: deposit/withdraw mutate the balance field; an over-draw
|
||||
raises `Insufficient` which the caller catches and falls back to
|
||||
the unchanged balance. Starting at 100, +50 = 150, -30 = 120,
|
||||
attempted -200 throws and the handler returns 120. Combines
|
||||
mutable record fields, user exception declaration, and
|
||||
try-with-bare-pattern in a realistic micro-pattern. 59 baseline
|
||||
programs total.
|
||||
- 2026-05-09 Phase 5.1 — bf_full.ml baseline (Brainfuck interpreter
|
||||
with `[`/`]` loops, `+++[.-]` → 3+2+1 = 6). Extends the iter-92
|
||||
brainfuck.ml subset with bracket matching: `[` jumps past matching
|
||||
|
||||
Reference in New Issue
Block a user