print and putStrLn are not interchangeable but are very, with putStrLn you need to convert to string first (with show), print takes any type with a show instance.
Read an integer
N
main :: IO ()
main = do
-- Read an integer from input
n <- readLn :: IO Int
-- Print the integer
print nRead an integer
Nalternative version
main :: IO ()
main = do
-- Read and convert the first line to an integer in one step
n <- read <$> getLine :: IO Int
print nRead a string
s, ignoring an integer
main :: IO ()
main = do
-- Read and ignore integer
readLn :: IO Int
s <- getLine
putStrLn $ sRead a list of integers separated by space with a function
import Data.List
import Data.Char
import qualified Data.ByteString.Char8 as B
getIntList :: IO [Int]
getIntList = fmap (unfoldr (B.readInt . B.dropWhile isSpace)) B.getLine
main :: IO ()
main = do
_ <- getLine
a <- getIntList
print $ aRead an integer and a list of integers separated by space without a function
main :: IO ()
main = do
-- Read an integer N
n <- readLn :: IO Int
-- Read a line with space-separated integers
-- equivalent to fmap (map read . words) getLine :: IO [Int]
listA <- map read . words <$> getLine :: IO [Int]
-- Print what was read
putStrLn $ "n = " ++ show n
putStrLn $ "List A = " ++ show listASame but with a different version of a function
getList :: Read a => IO [a]
getList = fmap (map read . words) getLine
solve :: Int -> Int -> Int
solve m n = m * n `div` 2
main :: IO ()
main = do
[m,n] <- getList
putStrLn . show $ solve m nRead a pair of integers
main :: IO ()
main = do
-- equivalent to map read . words <$> getLine :: IO [Int]
[a,b] <- fmap (map read . words) getLine :: IO [Int]
print a
print bRead a pair of integers then read n pairs
import Data.Maybe
import Control.Monad
import Data.List
main :: IO ()
main = do
[s, n] <- map read . words <$> getLine :: IO [Int]
a <- replicateM n $ (\[x,y] -> (x, y)) <$> (map read . words <$> getLine :: IO [Int])
print s
print n
print aRead an NxN matrix and print it
import Control.Monad (replicateM)
main :: IO ()
main = do
-- Read the first line as an integer N
n <- readLn :: IO Int
-- Read the NxN matrix
w <- readMatrix n
-- Print the results with debug strings
putStrLn $ "N = " ++ show n
putStrLn $ "Matrix W:"
printMatrix w
-- Helper function to read an NxN matrix
readMatrix :: Int -> IO [[Int]]
readMatrix 0 = return []
readMatrix n = do
-- Read N rows
replicateM n readRow
where
readRow = map read . words <$> getLine :: IO [Int]
-- Helper function to print a matrix
printMatrix :: [[Int]] -> IO ()
printMatrix matrix = do
mapM_ (\row -> putStrLn $ " " ++ show row) matrixRead t cases of one integer each case
import Control.Arrow
-- Ignore first line with the integer t and process input with interact
main = interact $
lines >>> drop 1 >>> map (read >>> solve >>> format) >>> concat >>> unlines-- Using replicateM_
main = do
n<- read <$> getLine ::IO Int
replicateM_ n onecase-- Using mapM_
import Data.List
main :: IO ()
main = do
getLine
input <- map (filter (/=0) . solve 0 . read) <$> lines <$> getContents :: IO [[Int]]
mapM_ f inputmain = do
t <- readLn :: IO Int
replicateM_ t $ do
n <- readLn :: IO Int
as <- map read . words <$> getLine :: IO [Int]
let r = myfun as 0 0 0 0 0
putStrLn r
return ()main :: IO ()
main = do
[t] <- getInts
replicateM_ t $ do
[n] <- getInts
as <- getInts
let go is@(i:is') ys@((j, y):ys')
| y >= j - i + 1 = go is ys'
| otherwise = (j - i) + go is' ys
go (i:is) [] = (n - i) + go is []
go [] _ = 0
print $ go [0..n-1] (zip [0..] as)
Example of interact vs replicate
draw :: [Int] -> [String]
draw (n:m:_) = take n $ cycle [full, right, full, left]
where full = replicate m '#'
left = '#':replicate (m-1) '.'
right = reverse left
solve :: String -> String
solve = unlines . draw . map read . words
main :: IO ()
main = interact $ solvedraw :: [Int] -> [String]
draw (n:m:_) = take n $ cycle [full, right, full, left]
where full = replicate m '#'
left = '#':replicate (m-1) '.'
right = reverse left
solve :: String -> [String]
solve = draw . map read . words
main :: IO ()
main = do
input <- getLine
mapM_ putStrLn $ solve inputExample of >>>
import Control.Arrow
main = interact $
lines >>> drop 1 >>> map (read >>> solve >>> format) >>> concat >>> unlinesExamples of >>=
import Data.List
distinct :: Ord a => [a] -> Bool
distinct = all ((== 1) . length) . group . sort
solve :: Int -> Int
solve y = head $ filter (distinct . show) [y + 1 ..]
main :: IO ()
main = getLine >>= putStrLn . show . solve . readmain :: IO ()
main = do
-- Read an integer N
n <- readLn :: IO Int
-- Read a line with space-separated integers using >>= (bind)
listA <- getLine >>= \line -> return (map read (words line) :: [Int])
-- Print what was read with debug strings
putStrLn "N:"
print n
putStrLn "List A:"
print listAUse of =<<
solve :: [Int] -> Int
solve [a,b] | a > b = 0
| otherwise = 1 + solve [a*3, b*2]
main :: IO ()
main = print . solve . map read . words =<< getLineUse of >> to ignore
main :: IO ()
main = getLine >> getIntList >>= print . solveExample of getContents
main :: IO ()
main = do
[k, l, m, n, d] <- getContents >>= return . map read . words :: IO [Integer]main :: IO ()
main = print . solve . map read . tail . words =<< getContentsmain :: IO ()
main = getContents >>= print . length . group . tail . lines