Second problem functional-style solution