[Haskell] Better Exception Handling

Lennart Augustsson lennart at augustsson.net
Fri Nov 26 05:11:11 EST 2004


Tomasz Zielonka wrote:
> On Thu, Nov 25, 2004 at 07:52:43PM +0100, Lennart Augustsson wrote:
> 
>>As I'm sure you have gathered from all the answers you can't have the
>>latter and keep Haskell pure.  But there is an interesting alternative
>>(at least theoretically).  You could have a function like
>>
>>mkCatchJust :: IO ((Exception -> Maybe b) -> (c -> a) -> c -> (b -> a) -> a)
> 
> 
> How is that different from this?
> 
>   mkReadFile :: IO (FilePath -> String)
> 
> This is wrong. Even if I get a function as a result of an IO computation, I
> expect that function to be pure.
> 

BTW, I couldn't stop myself.  Here's (a simple version of) mkReadFile:

mkReadFile :: IO (FilePath -> String)
mkReadFile = do
     names <- traverse "/"
     files <- mapM readFile names
     let find name =
	    case lookup name (zip names files) of
	    Just file -> file
	    Nothing -> error "file not found"
     return find

traverse :: FilePath -> IO [FilePath]
traverse d =
	do
	    fs <- getDirectoryContents d
	    let fs' = map ((d ++ "/") ++) (fs \\ [".", ".."])
	    fss <- mapM traverse fs'
	    return (concat fss)
     `catch` \ e ->
	return [d]

I don't really recommend it being used. :)

	-- Lennart


More information about the Haskell mailing list