Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Last active October 2, 2015 16:43
Show Gist options
  • Select an option

  • Save lucamolteni/07ee1e66864356dd29d5 to your computer and use it in GitHub Desktop.

Select an option

Save lucamolteni/07ee1e66864356dd29d5 to your computer and use it in GitHub Desktop.
Replicate
module Main where
import Control.Monad
main :: IO ()
main = do putStrLn "Enter the amount of line to read"
s <- readLn :: IO Int
putStrLn ("Now enter " ++ show s ++ " lines ")
elements <- replicateM s getLine
let result = doSomething elements
print result
return ()
doSomething :: [String] -> [String]
doSomething = map (++ "_operation on element")
@lucamolteni
Copy link
Copy Markdown
Author

Total version:

module Main where

import Control.Monad
import Text.Read

main :: IO ()
main = do putStrLn "Enter the amount of line to read"
          s <- readNumber
          case s of
              Just num -> inputLine num
              Nothing -> putStrLn "Not a valid number"
          return ()

inputLine :: Int -> IO ()
inputLine s = do putStrLn ("Now enter " ++ show s ++ " lines ")
                 elements <- replicateM s getLine
                 let result = doSomething elements
                 print result


readNumber :: IO (Maybe Int)
readNumber = do s <- getLine
                let a = readMaybe s :: Maybe Int
                return a

doSomething :: [String] -> [String]
doSomething = map (++ "_operation on element")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment