[Haskell-beginners] Use of interact

Michele Alzetta michele.alzetta at gmail.com
Sun Feb 10 16:10:13 UTC 2019


I solved the hacker-rank hello world n times challenge thus:

hello_worlds :: Int -> IO ()
hello_worlds n
| n < 1 = return ()
| otherwise = do
putStrLn "Hello World"
hello_worlds (n-1)

main :: IO()
main = do
n <- readLn :: IO Int
hello_worlds n

I would like to solve this by using the interact function.
If I leave my hello_worlds function as is and change the main function as
follows:

main = interact $ show . hello_worlds . read::Int

I get:

Couldn't match expected type ‘IO t0’ with actual type ‘Int’
    • In the expression: main
      When checking the type of the IO action ‘main’

helloworlds.hs:14:8-44: error:
    • Couldn't match expected type ‘Int’ with actual type ‘IO ()’
    • In the expression: interact $ show . hello_worlds . read :: Int
      In an equation for ‘main’:
          main = interact $ show . hello_worlds . read :: Int

could someone please explain why this can't work?
Is it possible to use interact in such a context?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20190210/21c11099/attachment.html>


More information about the Beginners mailing list