Pattern matching and scala for expressions

Please let me know if the code should be changed in any way.