Skip to content

Instantly share code, notes, and snippets.

@o-sam-o
Created August 30, 2011 10:25
Show Gist options
  • Save o-sam-o/1180613 to your computer and use it in GitHub Desktop.
Save o-sam-o/1180613 to your computer and use it in GitHub Desktop.
Haskell Compile Question
import Control.Monad
-- Compiles
loadPaths :: FilePath -> IO [Int]
loadPaths filename = do strings <- liftM lines $ readFile filename
return (map read strings)
-- Doesn't Compile
loadPaths :: FilePath -> IO [Int]
loadPaths filename = do strings <- liftM lines $ readFile filename
map read strings
@md2perpe
Copy link

Do you need an explanation of why the second one doesn't compile?

@o-sam-o
Copy link
Author

o-sam-o commented Aug 31, 2011

Yes, also is the use of liftM a good idea?

@md2perpe
Copy link

In a do statement for the monad m every line (exception: let lines; see below) needs to have a type in the monad, i.e. m a for some a.
In the second code, the last line has a type of [Int], which is not in the monad. Using return lifts the value to the monad so that the type is IO [Int].

Using liftM is okey, but could be avoided using let:

loadPaths filename = do content <- readFile filename
                        let strings = lines content
                        return (map read strings)

@o-sam-o
Copy link
Author

o-sam-o commented Aug 31, 2011

Thank!

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