Why is type inference failing here?

Make prev_x a Ref or use a loop instead.
I think the loop is cleaner.