Refactor `walk` function using pattern matching

Pattern matching is a helpful tool that makes code clear, safe and shorter.