1

It may be a dumb question but I got an error when I try to get an input outside of the main function:

menu2Players :: String -> String -> (String, String)
menu2Players player1 player2 = do
    putStrLn("Qual o nome do primeiro jogador?\n")
    player1 <- getLine
    putStrLn("Qual o nome do segundo jogador?\n")
    player2 <- getLine
    return (player1, player2)

The error I got

The IO action ‘main’ is not defined in module ‘Main’

What can I do to fix this?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Some
  • 21
  • 3
  • 9
    Your function actually returns a value of type `IO (String, String)`, so your type signature will cause an error. The lack of `main` is an entirely separate issue, but if you try to actually compile a file rather than just load it into GHCi, it needs to have a `main` value. – Robin Zigmond Oct 14 '19 at 22:15
  • The program has a main but I want to return this function to another function – Some Oct 14 '19 at 22:17
  • Well, that shouldn't be a problem. It must be a function (or, _action_) of `IO` type, though. – leftaroundabout Oct 14 '19 at 22:35
  • 2
    The code you posted and the error presented are unrelated. – Thomas M. DuBuisson Oct 15 '19 at 00:53
  • `return` in Haskell is not the same `return` you'll find in other languages. It's a function, not a keyword, and as such has no effect on control flow. [Quick demonstration](https://tio.run/##TY6xDoJAEER7vmJKLeiMBdFYU1FYWBNYZOOxR469cH79eQhBt5p9mbxMX08vMibGznpXuTsHFAXKCqVo9mNXtDZDOkfqneC05DzHg42B9jyh89IoWwEFVvAwUMu1knnDj4lORCzPtWpY6PZvO2@2ymFehEnQWFEWT1CLnlzqZ0PNso07HNdvnxVwybHP/aLRsShCjB8) – Khuldraeseth na'Barya Oct 15 '19 at 03:25

2 Answers2

1

In Haskell, if you want to do IO in a function, it needs to return IO.

menu2Players :: String -> String -> IO (String, String)
menu2Players player1 player2 = do
    putStrLn "Qual o nome do primeiro jogador?\n"
    player1 <- getLine
    putStrLn "Qual o nome do segundo jogador?\n"
    player2 <- getLine
    return (player1, player2)

The details of why you need to return IO are a little tricky, but you can learn about it here: http://learnyouahaskell.com/input-and-output

Garrison
  • 386
  • 2
  • 8
0

(a bit late, but...) The code that worked, I just neeed the IO return.

menu2Players = do
    putStrLn "Qual o nome do primeiro jogador?\n"
    player1 <- getLine
    putStrLn "Qual o nome do segundo jogador?\n"
    player2 <- getLine
    return (player1, player2)
Some
  • 21
  • 3