haskell icon indicating copy to clipboard operation
haskell copied to clipboard

Series exercise requires strange edge case handling

Open mgw93 opened this issue 2 years ago • 1 comments

The test cases for the Series exercises require that series 0 [] = [[]] series _ [] = []

This seems counterintuitive and is not documented in the problem description. Is there a reason why the test cases were chosen like this?

mgw93 avatar Feb 26 '22 11:02 mgw93

For series n xs, you would expect that length (series n xs) == min 0 (length xs - n + 1)

If n == 0 and length xs == 0, then length (series n xs) should be 0 - 0 + 1 == 1. This tells us that there is one way to make a series of length 0 from the empty series: the empty series.

If n == 1 and length xs == 0, then length (series n xs) should be 0 - 1 + 1 == 0. This tells us that the empty series contains no series of length 1.

If n == 2 and length xs == 0, then length (series n xs) should be 0 because 0 - 2 + 1 < 0. This too tells us that the empty series contains no series of length 2.

I'm not sure I feel particularly great about the test cases with n == 0 though. They could all be removed, but then someone would just suggest we add them again.

It's probably better to pick exactly one of these two options:

  • Declare that n == 0 is an error. Therefore series be Int -> String -> Maybe [[Int]], and the result for n == 0 should be Nothing.
  • Make the description better by submitting a pull request for https://github.com/exercism/problem-specifications/blob/main/exercises/series/description.md

petertseng avatar Feb 26 '22 23:02 petertseng