REVISED: Wednesday, January 24, 2024
Haskell source code example: Reading From A File.
I. HASKELL SOURCE CODE EXAMPLE OF READING FROM A FILE
A. CREATE A FILE
"Copy Paste" the following paragraph into your text editor:
Mary had a little lamb,
its fleas were white as snow.
And everywhere that Mary went,
its fleas were sure to go.
From your text editor do a "File, Save As"
txtFileIn.txt
B. WRITE HASKELL PROGRAM
"Copy Paste" the following Haskell program into your text editor:
module Main where
main :: IO ()
main = do x <- readFile "txtFileIn.txt"
putStrLn x
From your text editor do a "File, Save As"
Main.hs
Notice, neither readFile nor writeFile ever provide a Handle for you to work with, so there is nothing to ever hClose.
readFile should be used instead of opening, reading, and closing a file.
C. LOAD HASKELL PROGRAM INTO GHCi INTERPRETER
After the GHCi Prelude> prompt type :load Main as shown below, then press Enter:
Prelude> :load Main
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
D. COMPILE HASKELL PROGRAM
After the GHCi Prelude> prompt type :! ghc --make "*Main" as shown below, then press Enter:
[1 of 1] Compiling Main ( Main.hs, Main.o )
Linking Main.exe ...
E. EXECUTE COMPILED HASKELL PROGRAM
Navigate to your working directory window. Hold down the Shift key and right mouse click in any open area of the window. Select the "Open command window here" option. The following path will be filled in for you:
C:\Users\Tinnel\Haskell2024>
After the > prompt type Main.exe and press Enter and the compiled program will be executed as shown below:
C:\Users\Tinnel\Haskell2024>Main.exe
Mary had a little lamb,
its fleas were white as snow.
And everywhere that Mary went,
its fleas were sure to go.
C:\Users\Tinnel\Haskell2024>
II. HASKELL GHCI INTERPRETER SOURCE CODE TO READ A FILE
A. MAIN
Prelude> :load Main
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, modules loaded: Main.
Prelude>
Prelude> main
Mary had a little lamb,
its fleas were white as snow.
And everywhere that Mary went,
its fleas were sure to go.
Prelude>
B. READFILE
Prelude> readFile "txtFileIn.txt"
"Mary had a little lamb,\nits fleas were white as snow.\nAnd everywhere that Mary went,\nits fleas were sure to go."
it :: String -- Remember this prints because of "options currently set: +t"
C. :EDIT
Prelude> :edit "txtFileIn.txt"
Ok, modules loaded: Main.
The above :edit "txtFileIn.txt" code will open a Notepad window with the following displayed:
its fleas were white as snow.
And everywhere that Mary went,
its fleas were sure to go.
III. REFERENCES
Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.
Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.
Goerzen, J. & O'Sullivan, B. & Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.
Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.
Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.
Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.
In this tutorial, you have received an introduction to reading from a file.