I need some help understanding a Haskell template of the "List Replication" Hackerrank challenge. I do not understand the order in which the functions are executed.
f = concatMap . replicate -- This part handles the Input and Output and can be used as it is. main :: IO () main = getContents >>= mapM_ print . (\(n:arr) -> f n arr) . map read . words The getContents function should produce an IO String like "2\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10". I understand roughly what happens next, but I do not understand in what order and with which precedence. I tried to execute words "2\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10 in ghci and to feed the result into map read. But then I get the result "[*** Exception: Prelude.read: no parse". If I try map read . words "2\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10" I get the result "Couldn't match expected type ‘a -> [String]’ with actual type ‘[String]’". How is it that the whole composed main function throws no errors?
I would very much appreciate if someone could help me understand the whole main function. Especially which functions get executed in which order with which input, and how I could slice it in smaller (working!) parts to understand it better.
Many thanks!
readand GHC can see what is the one you want from the context. GHCi, without the proper context, infers the wrong one and causes the crash. Inconvenient, but that's what it happened.