How to search for a string sequence in a file a rewrite it???

Tom Pledger
Fri, 14 Mar 2003 09:59:53 +1300

(moving to haskell-cafe)

Alexandre Weffort Thenorio writes:
 | Ooops a small error before but here is the right one.
 | Great. I got almost everything. My problem now is:
 | I got a function called findstr where
 | findstr "aaaa" "zzzzz" ["xxxxaaaa","xxxaaaaxxx"] =
 | ["xxxxzzzzz","xxxzzzzzxxx"]

with the inferred type
findstr :: String -> String -> [String] -> [String]

 | and then I try something like
 | fillIn lines = do
 |   bkfilled <- (findstr str str2 lines)

with the inferred type
(findstr str str2 lines) :: [String]

Note that [] (the list data constructor) is a monad.  So, this 'do'
expression is in the [] monad, where perhaps you intended the IO
monad.  Computations in the [] monad have the effect of iterating over
the elements of lists.  So, the bound variable gets the inferred type

bkfilled :: String

which you've noticed in the error message.

 |   write bkfilled
 | where write takes a [String] and concatenates it writing it to a file then.
 | But I get this error saying:
 | Expected Type: [String]
 | Inferred Type: String
 | In the first argument of 'write' namelly 'bkfilled'
 | In a do expection pattern binding: write bkfilled
 | Any idea?? I mean bkfilled is supposed to be [String] but it says it is a
 | String, any idea why???

The smallest code change to fix this is to add a 'return', which will
wrap another monad (in this case, IO) around findstr's [String] result...

> fillIn lines = do
>   bkfilled <- return (findstr str str2 lines)
>   write bkfilled

... but you should be able to simplify that code, using one of the
monad laws in