문제

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 
도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top