[Haskell-beginners] Re: problem with System.Directory.Tree

Daniel Fischer daniel.is.fischer at web.de
Tue Jul 6 03:53:48 EDT 2010


On Tuesday 06 July 2010 04:01:58, Anand Mitra wrote:
> All the advice and help I got on my difficulties till now have been
> very useful. My current problem is a little weird and can't
> figure out what is happening.
>
> I have been able to get the serialization working with DirTree based
> on the suggestions I have received till now. I have a function calcMD5
> which given a FilePath will traverse the entire tree calculating the
> checksum of each file it encounters. The resultant structure is
> serializable by encode. But when I do a encodeFile to store the result
> to a file I get nothing.
>
> ,----
>
> | *Main> calcMD5 "/tmp/tmp"
> | AncTree "/tmp" (DirW {name = "tmp", contents = [FileW {name =
> | "passwd",
>
> file = Prop {md5sum = f54e7cef69973cecdce3c923da2f9222, modTime = Tue
> Jul  6 07:18:16 IST 2010, filenam = "/tmp/tmp/passwd"}}]})
>
> | *Main> liftM encode $ calcMD5 "/tmp/tmp"
> | Chunk
>
> "\NUL\NUL\NUL\NUL\NUL\NUL\NUL\EOT/tmp\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\ET
>Xtmp\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SOH\SOH\NUL\NUL\NUL\NUL\NUL\NUL\NUL\ACKp
>asswd\NUL\245N|\239i\151<\236\220\227\201#\218/\146\"\NULL2\139`\NUL\NUL\
>NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\NUL\SI/tmp/tmp/passwd" Empty
> `----
>
> clearly the encoding is working.
> But if I try to use encodeFile to write it to a file the file is not
> created.
>
> ,----
>
> | *Main> liftM (encodeFile "/tmp/tmp-list") $ calcMD5 "/tmp/tmp"

calcMD5 :: IO something

I suppose?

then

liftM (encodeFile "/tmp/tmp-list") $ calcMD5 "/tmp/tmp"

has type

IO (IO ())

and executing that only evaluates the action 
(encodeFile "/tmp/tmp-list" calcMD5Result)
, it doesn't execute it.

What you want is

calcMD5 "/tmp/tmp" >>= encodeFile "/tmp/tmp-list"

> |
> | $ ls /tmp/tmp-list
> | ls: cannot access /tmp/tmp-list: No such file or directory
>
> `----
>
> I tried a few other things like converting the Bytestring from encode
> into a string using show and then doing a writeFile on
> it. Unfortunately none of them worked.
>
> regards



More information about the Beginners mailing list