Implement `sum` function using helper function