redid problem 4 in a functional manner