removed unnecessary nested function and use pattern matching only in hw2 problem 1a