Created
August 30, 2011 10:25
-
-
Save o-sam-o/1180613 to your computer and use it in GitHub Desktop.
Haskell Compile Question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Yes, also is the use of liftM a good idea?
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)
Thank!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you need an explanation of why the second one doesn't compile?