I am trying to learn Haskell doing some contest problems. I have worked a bit with the language but still have a very long way ahead.
Right now I am working with a problem called Ants:
An army of ants walk on a horizontal pole of length l cm, each with a constant speed of 1 cm/s. When a walking ant reaches an end of the pole, it immediatelly falls off it. When two ants meet they turn back and start walking in opposite directions. We know the original positions of ants on the pole, unfortunately, we do not know the directions in which the ants are walking. Your task is to compute the earliest and the latest possible times needed for all ants to fall off the pole.
The first line of input contains one integer giving the number of cases that follow, at most 100. The data for each case start with two integer numbers: the length l of the pole (in cm) and n, the number of ants residing on the pole. These two numbers are followed by n integers giving the position of each ant on the pole as the distance measured from the left end of the pole, in no particular order. All input integers are between 0 and 1000000 and they are separated by whitespace.
For each case of input, output two numbers separated by a single space. The first number is the earliest possible time when all ants fall off the pole (if the directions of their walks are chosen appropriately) and the second number is the latest possible such time.
2 10 3 2 6 7 214 7 11 12 7 13 176 23 191
It is pretty straight forward, some parsing and minor calculation. My program finished running in 0.69 seconds, while the best result someone achieved with Haskell is something like 0.03! It is very annoying.
I have tried to speed it up with some even uglier code, but the best I have managed with this is 0.66 seconds.
Can anyone spot some obvious things about this code to improve the runtime?
main = do contents <- getContents let cases = parse contents mapM_ putStrLn (map showcase cases) type Case = (Int, [Int]) showcase :: Case -> String showcase (len, positions) = let ds = dists len positions edgiest = (snd . maximum) ds es = max (len - edgiest) edgiest midmost = (snd . minimum) ds mm = min (len - midmost) midmost in show mm ++ " " ++ show es dists :: Int -> [Int] -> [(Float, Int)] dists l = map (\x -> (abs (fromIntegral x - m), x)) where m = fromIntegral l / 2.0 parse :: String -> [Case] parse = parseCases . tail . words -- skip the first integer parseCases :: [String] -> [Case] parseCases  =  parseCases xs = let (cs, rest) = parseCase xs in cs : parseCases rest parseCase :: [String] -> (Case, [String]) parseCase (len:a:xs) = let ants = read a positions = (map read . take ants) xs rest = drop ants xs in ((read len, positions), rest)