Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Best practice for catching pure errors in IO?
      (Aleksandar Dimitrov)
   2. Re:  Help-me read file (Tim Perry)


----------------------------------------------------------------------

Message: 1
Date: Wed, 27 Oct 2010 22:06:04 +0200
From: "Aleksandar Dimitrov" <[email protected]>
Subject: Re: [Haskell-beginners] Best practice for catching pure
        errors in IO?
To: "John Smith" <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <op.vk83gedkjdv...@bylha>
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes

> I have a pure value which may throw errors when it is evaluated in an IO  
> monad. My code to handle it looks like this:

"Throwing" errors (i.e. calling the `error` function from pure code) isn't  
something you should do except in cases you're *very* sure that there is  
*no* way for the program to recover ever again. Maybe and Either are  
better alternatives. `try` basically just forces using the cleaner way of  
handling exceptions (Either) when you have to deal with something (say,  
library code) that you know may call `error` (Prelude's `head` does that  
when given an empty list) but you'd prefer to recover more gracefully. If  
you have written the code yourself and you *know* you can recover,  
throwing an exception and recovering with `try` is not pretty. That's  
good, you don't want it to be pretty, you want it to hurt, since it's not  
optimal. You should instead make your `evaluate` have a signature like  
this: Value -> Either ErrorMessage Result, where ErrorMessage is probably  
`type`d to String.

> temp <- try $ evaluate value
> case temp of
>       Left (error::ErrorCall) -> scoldTheUserAndTryAgain
>       Right correct -> doSomething
>
> Can this be done without the temporary variable, or with less plumbing?  
> I tried lifting either, but GHC was fussing about ambiguous types,  
> appending a signature to a pattern match is no longer allowed(?), and it  
> didn't look that much cleaner.

It can be done without a temporary variable. You can write a function like  
this (scoldTheUserWith and tryToObtainAResult should be in the IO Monad,  
of course!)

> treat :: (Either ErrorMessage Result) -> IO Result
> treat (Left e) = scoldTheUserWith e >> tryToObtainAResult
> treat (Right r) = return r

Be careful, the result types of treat have to be the same for Left and  
Right! Now do something like this, inside the IO monad:

> tryToObtainAResult :: IO Result
> tryToObtainAResult = do       result <- treat $ evaluate value
>       doSomethingWith result

I hope this doesn't contain errors, I was too lazy to check it :-P Also,  
it can perhaps be done more gracefully.
HTH,
Aleks

PS: GHC might complain about appending a type signature in pattern matches  
if you have something like this:

> import Control.Monad (liftM)
> main = do r :: Integer <- liftM read getLine
>           print $ show r

You can fix line 2 in 2 ways:

> main = do r <- liftM read hGetLine :: IO Integer

Or compiling with -XScopedTypeVariables (or setting {-# LANGUAGE  
ScopedTypeVariables #-} as the very first line in your file.)


------------------------------

Message: 2
Date: Wed, 27 Oct 2010 17:31:11 -0700 (PDT)
From: Tim Perry <[email protected]>
Subject: Re: [Haskell-beginners] Help-me read file
To: Gilmara Pompelli <[email protected]>,
        [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi Gilmara,

I assume you are trying to do this:

main = do
          map sort $ learquivo "dataFile.txt"

which gives you the error you mention. The problem is you are trying to use a 
"IO [[Int]]" where you want a "[[Int]]". To data out of IO, you must use the 
"<-" operator. To write a program which reads a line from the user and echos it 
back you do this:
main = do
        userLine <- readLine
        putStrLn userLine

The type of userLine is "String" (or "[Char]" if you prefer).

Similarly, to change your "IO [[Int]]" to an [[Int]] you need to use the <- 
operator.
So your program could become:

main = do 
        intListOfLists <- learquivo "dataFile.txt"
let sortedSubLists = map Data.List.sort intListOfLists
        .....

Does this help? If not, read the "True Nature of Return" section of Real World 
Haskell: http://book.realworldhaskell.org/read/io.html#io.return

Hope this helps,
Tim


----- Original Message ----
From: Gilmara Pompelli <[email protected]>
To: [email protected]
Sent: Mon, October 18, 2010 12:20:21 PM
Subject: [Haskell-beginners] Help-me read file

Hello

I have a file with 100 lists, with 100 ints.

I have to read the file and apply the map and sort functions on lists.

II did it to read file:

learquivo :: FilePath -> IO ([[Int]])
learquivo s = do
            conteudo <- readFile s
            return (read conteudo)

But now applying the sort, the error appears:


<interactive>:1:5:
    Couldn't match expected type `[a]'
           against inferred type `IO [[Int]]'

how can I fix?

Thanks
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners



------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 28, Issue 42
*****************************************

Reply via email to