string - Haskell types missmatch -


i want make function takes string "path" path of file has 1 line, want take line , check if it's proper expression , if build tree out of string, here code `

loadexpression :: string -> tree char loadexpression path =  contents <- readfile path  if checkifproper $ filter (/=' ') contents     buildtreefromstring contents     else emptytree   

`

but gives me error "couldn't match type io' withtree' " . know io string different normal 1 isn't <- suppost ? converting io string normal one. if call buildtreefromstring string "(1+2)*3" works fine, same checkifproper.

the whole error :

couldn't match type `io' `tree' expected type: tree string   actual type: io string in return type of call of `readfile' in stmt of 'do' block: contents <- readfile path 

readfile has type filepath -> io string, do block in io monad. entire function therefore returns io (tree char), not tree char, need change type signature.

edit: can separate effectual , pure parts of function, creating function load tree input string. can pass string readfile function:

readtree :: string -> tree char readtree contents =  if checkifproper $ filter (/=' ') contents     buildtreefromstring contents     else emptytree 

loadexpression becomes:

loadexpression :: filepath -> io (tree char) loadexpression path =   contents <- readfile path   return (readtree contents) 

or can use fmap:

loadexpression = fmap readtree readfile 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -