First Haskell IO program isn't working

Elliot Gorokhovsky

Sorry, this is probably really dumb, but can someone explain me why this program doesn't compile? I get Couldn't match expected type 'a1 -> String' with actual type 'IO String'.

import System.Environment

main = do
  [first, last] <- getArgs
  firstnames <- lines . readFile "firstnames_male"
  lastnames <- lines . readFile "lastnames"
  print firstnames
MathematicalOrchid

You can't do lines . readFile "lastnames".

The readFile function returns an IO String, not a String.

You can, however, use the fmap function (or the <$> operator) to achieve this:

main = do
  [first, last] <- argArgs
  firstnames <- lines `fmap` readFile "firstnames_males"
  ...

This works because IO is a functor.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related