Programming Praxis – The Mod Out System

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.

Our import:

import Data.List.Split

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.

Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: