Today’s Programming Praxis problem is an easy one: all we have to do is convert a string that contains numbers and number ranges to a full list of numbers. The original PL/SQL solution is 49 lines, and the scheme solution has 9. Let’s see if we can bring that down further still.
What we have to do is pretty simple: first we split the string on the commas. The resulting chunks are split on the dash. The resulting numbers are converted to Ints. Since we need two numbers to define a range, we cycle these numbers, and take the range between the first two numbers.
modOut :: String -> [Int] modOut = concatMap ((\(a:b:_) -> [a..b]) . cycle . map read . sepBy "-") . sepBy ","
A quick test reveals that everything’s working correctly:
main :: IO () main = print $ modOut "1-6,9,13-19"
And so we’ve reduced the solution size by another factor of three. That will do nicely.