Question

First there is an integer(t) that indicates the number of test case. Then 2*t lines follow. In each line there is a integer. We have to output the sum of each two numbers.

sample input:

3
1
2
3
4
5
6

sample output:

3
7
11 
Was it helpful?

Solution

import Control.Monad (replicateM)

main :: IO ()
main = mapM_ print . map (uncurry (+)) =<< flip replicateM readIntPair =<< readLn

readIntPair :: IO (Integer, Integer)
readIntPair = do
    x <- readLn
    y <- readLn
    return (x, y)

replicateM is from Control.Monad, the other functions are imported automatically from the Prelude.

You will also want to read part of a tutorial that explains do notation: it looks the same as, but is subtly different from, a list of instructions in an imperative language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top