serialization - Serializing data structures to a file with Haskell? -


i'm new haskell , i'm trying figure out how io works. have data structure, tree, want serialize file , de-serialize out data structure. seems should able through show , read, use of read throwing error. here's relevant part of code:

data tree = answer string | question string tree tree deriving (read, show)  filetotree :: (read a) => filepath -> io filetotree filepath =      datastruct <- readfile filepath     return (read datastruct)  treetofile :: (show a) => -> filepath -> io () treetofile datastruct filepath =     writefile filepath (show datastruct)  main =     let filepath = "data.txt"     let ds = filetotree filepath     ask ds     treetofile ds filepath  ask :: tree -> io () ask (question q yes no) =     putstrln q     answer <- getline 

the error i'm getting "couldn't match expected type 'tree' against inferred type 'io a' in first argument of 'ask'". seems read should return tree type, it's returning io type. there way coerce tree type, or totally mis-reading problem?

note filetotree returns value in io. means line let ds = filetotree filepath binding io a ds identifier. if want work on value obtain in io monad, need use >>= (bind) function io monad replacing let ds = filetotree filepath line ds <- filetotree filepath. bind of type a ds, in sense stripping off outer io. call ask unify a tree.

with out of way, there seems issue ask. have above doesn't compile, , seems though not using answer, guessing error in pasting code.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -