-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Haskell Data Analysis cookbook
By :
Making sure our code doesn't crash in the process of data mining or analysis is a substantially genuine concern. Some computations may take hours, if not days. Haskell gifts us with type safety and strong checks to help ensure a program will not fail, but we must also take care to double-check edge cases where faults may occur.
For instance, a program may crash ungracefully if the local file path is not found. In the previous recipe, there was a strong dependency on the existence of input.txt in our code. If the program is unable to find the file, it will produce the following error:
mycode: input.txt: openFile: does not exist (No such file or directory)
Naturally, we should decouple the file path dependency by enabling the user to specify his/her file path as well as by not crashing in the event that the file is not found.
Consider the following revision of the source code.
Create a new file, name it Main.hs, and perform the following steps:
import Control.Exception (catch, SomeException)
import System.Environment (getArgs)
main as follows:main :: IO () main = do
fileName string depending on the user-provided argument, defaulting to input.txt if there is no argument. The argument is obtained by retrieving an array of strings from the library function, getArgs :: IO [String], as shown in the following steps:args <- getArgs
let filename = case args of
(a:_) -> a
_ -> "input.txt"readFile on this path, but catch any errors using the library's catch :: Exception e => IO a -> (e -> IO a) -> IO a function. The first argument to catch is the computation to run, and the second argument is the handler to invoke if an exception is raised, as shown in the following commands: input <- catch (readFile fileName)
$ \err -> print (err::SomeException) >> return ""input string will be empty if there were any errors reading the file. We can now use input for any purpose using the following command:print $ countWords input
countWords function as follows:countWords input = map (length.words) (lines input)
This recipe demonstrates two ways to catch errors, listed as follows:
args list is empty, and the last pattern, "_", is caught, resulting in a default filename of input.txt.input to an empty string.Conveniently, Haskell also comes with a doesFileExist :: FilePath -> IO Bool function from the System.Directory module. We can simplify the preceding code by modifying the input <- … line. It can be replaced with the following snippet of code:
exists <- doesFileExist filename input <- if exists then readFile filename else return ""
In this case, the code reads the file as an input only if it exists. Do not forget to add the following import line at the top of the source code:
import System.Directory (doesFileExist)
Change the font size
Change margin width
Change background colour