简体   繁体   中英

First Haskell IO program isn't working

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

You can't do lines . readFile "lastnames" 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM