Would a recursive definition be a clearer way to implement this (at least the first time)? Putting focus on references, pattern matching and `ref`, without having to deal with the `mut` bindings and `loop` and so on.

(One might then have the iterative definition to demonstrate the "correct" way, given our lack of mandated tail-call elimination.)