Fix exercise 5.1 generating function