[Haskell-beginners] why is something different within a function when it comes out?

Michael Mossey mpm at alumni.caltech.edu
Wed Jul 14 04:10:04 EDT 2010


I'm fairly new, so I can't fully explain the behavior you got, but I 
know at least one thing you did wrong:

prad wrote:
> i have this:
> 
> 
> main = do
>     
>     c <- readFile "test.txt"
>     let tleP = "<title>\n(.*)\n</title>"
>     let tle = c=~tleP::[[String]]
>     putStrLn $ tle!!0!!1
> 
>     let g = xtract tleP c
>     putStrLn $ show g 
> 
> 
> xtract p c = do
>     let r = c=~p::[[String]]
>     return r!!0!!0!!1
> 

You probably mean to write

 > xtract p c = r !! 0 !! 1
 >   where r = c=~p::[[String]]

You used do-notation when you meant to write a simple function.

The use of "return" in do-notation is one point of confusion with 
beginners. It does not operate like "return" in an imperative language.

I'm not sure what your 'xtract' did, but the compiler probably accepted 
the do-notation because it specified a list monad of some sort.

I recommend you pay close attention to introductory examples, noticing 
in particular when they are unlike imperative languages. Find some 
tutorials on monads and the do-notation.

Best,
Mike


More information about the Beginners mailing list