Skip to content

Instantly share code, notes, and snippets.

@Vicfred
Last active March 18, 2025 05:42
Show Gist options
  • Select an option

  • Save Vicfred/9faad35e97f1e420a05f73f69e4a6495 to your computer and use it in GitHub Desktop.

Select an option

Save Vicfred/9faad35e97f1e420a05f73f69e4a6495 to your computer and use it in GitHub Desktop.
reading input from command line in haskell

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 n

Read an integer N alternative version

main :: IO ()
main = do
  -- Read and convert the first line to an integer in one step
  n <- read <$> getLine :: IO Int
  print n

Read a string s, ignoring an integer

main :: IO ()
main = do
  -- Read and ignore integer
  readLn :: IO Int
  s <- getLine
  putStrLn $ s

Read 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 $ a

Read 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 listA

Same 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 n

Read 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 b

Read 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 a

Read 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) matrix

Read 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 input
main = 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)
 

Misc

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 $ solve
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 = draw . map read . words

main :: IO ()
main = do
    input <- getLine
    mapM_ putStrLn $ solve input

Example of >>>

import Control.Arrow

main = interact $
  lines >>> drop 1 >>> map (read >>> solve >>> format) >>> concat >>> unlines

Examples 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 . read
main :: 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 listA

Use 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 =<< getLine

Use of >> to ignore

main :: IO ()
main = getLine >> getIntList >>= print . solve

Example 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 =<< getContents
main :: IO ()
main = getContents >>= print . length . group . tail . lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment