I suspect what confused you here is the assumption that scanners work like input streams: once you execute (readc stream), the stream state changes. However, scanners are functional and are equivalent to monads in this case; if you keep the old version of the scanner, it's still the old state of the scanner that you'll use.