[Haskell-beginners] Re: monad nomad gonad gomad

Michael Mossey mpm at alumni.caltech.edu
Sat Aug 14 21:28:03 EDT 2010


For your string-manipulation problem, I don't think you meant to use 
do-notation. The compiler accepted it because sees it as the list monad, 
and as a coincidence of the way you wrote it, it did what you expected.

Try this, using (let ... in ) syntax.

 > mkTxt :: (IConnection conn) => conn -> String -> [String]
 > mkTxt conn tS =
 >     let wL          = words (rpNls tS)
 >         ((f,vL):zz) = gtInx wL ["```"]
 >         rvL         = reverse vL
 >     in doIns wL rvL
 >         where doIns wL []     = wL
 >               doIns wL (v:vs) =
 >                   let (f,a:b:ss) = splitAt v wL
 >                   in (doIns f vs) ++ ["aoeeuu"] ++ ss

(Untested.)

Next readFile has the signature
readFile :: FilePath -> IO String

So the way I think of it is that any function using it has to return a 
result of "IO <something>"

You would have to call your pure function mkTxt from inside a monadic 
computation:

run :: IO ()
run = do
   zzzz <- readFile "zzpubs.txt"
   ...
   -- assuming mkTxt is modified to accept a third arg
   let result = mkTxt conn ts zzzz
   print result

Finally, yes Haskell complains about type-related faults in identifiers you 
don't use... because it is trying to help you find your mistakes. How does 
it know it wasn't a mistake on your part?

This is an advantage over script languages.

Mike


prad wrote:
> On Fri, 13 Aug 2010 21:39:33 -0700
> prad <prad at towardsfreedom.com> wrote:
> 
>> "I think It's time for you to get serious with the monads"
>> that's just what i'm going to do!
>>
> i'm asking the question in this thread because i think it has something
> to do with monads though i'm not sure. in fact, the problem seems
> completely bizarre to me.
> 
> i have a function:
> 
> mkTxt :: (IConnection conn) => conn -> String -> [String]
> mkTxt conn tS = do
>     --zzzz <- readFile "zzpubs.htm"
>     let wL          = words (rpNls tS)
>         ((f,vL):zz) = gtInx wL ["```"]
>         rvL         = reverse vL
>     doIns wL rvL
>         where doIns wL []     = wL
>               doIns wL (v:vs) = do
>                   let (f,a:b:ss) = splitAt v wL
>                   (doIns f vs) ++ ["aoeeuu"] ++ ss
> 
> the program compiles and runs fine.
> however, if i remove the comment dashes to allow
> zzzz <- readFile "zzpubs.htm"
> 
> the compiler produces what is to me an incomprehensible rationale for
> an error:
> 
> ====
>  gadit.hs:103:4:
>     Couldn't match expected type `IO String'
>            against inferred type `[String]'
>     In a stmt of a 'do' expression: zzzz <- readFile "zzpubs.htm"
>     In the expression:
>         do { zzzz <- readFile "zzpubs.htm";
>              let wL = words (rpNls tS)
>                  ((f, vL) : zz) = gtInx wL ...
>                  ....;
>              doIns wL rvL }
>     In the definition of `mkTxt':
>         mkTxt conn tS
>                 = do { zzzz <- readFile "zzpubs.htm";
>                        let wL = ...
>                            ....;
>                        doIns wL rvL }
>                 where
>                     doIns wL [] = wL
>                     doIns wL (v : vs)
>                             = do { let ...;
>                                    .... }
> ====
> 
> i don't do anything with zzzz!!
> it merely is the name i'm giving to the monadic computation to read in
> a file. in fact, it has nothing to do with the rest of the function
> because i don't use it at all.
> 
> why is the compiler complaining?
> 
> 


More information about the Beginners mailing list