readFile
has type FilePath -> IO String
, so your do
block is in the IO
monad. Your entire function therefore returns an IO (Tree Char)
, not a Tree Char
, so you need to change your type signature.
EDIT: You can separate the effectual and pure parts of your function, by creating a function to load the tree from an input string. You can then pass the string from readFile
into this function:
readTree :: String -> Tree Char
readTree contents =
if checkIfProper $ filter (/=' ') contents
then buildTreeFromString contents
else EmptyTree
loadExpression
then becomes:
loadExpression :: FilePath -> IO (Tree Char)
loadExpression path = do
contents <- readFile path
return (readTree contents)
or you can use fmap
:
loadExpression = fmap readTree readFile