REVISED: Saturday, February 10, 2024
Haskell SOURCE CODE EXAMPLE: Writing To A File.
I. HASKELL SOURCE CODE EXAMPLE OF WRITING TO A FILE
When we execute a Haskell program, the "main function" of the "Main module" gets called.
A. WRITE HASKELL PROGRAM
For example; "Copy Paste" the following Haskell program into your text editor:
module Main where -- Capital M
main :: IO () -- Lowercase m
main = writeFile "C:/Users/Tinnel/Haskell2024/txtFileOut.txt" oops
oops = "A hearse. A hearse. My kingdom for a hearse."
From your text editor select "File, Save As", and save the file as:
Main.hs -- Capital M
Notice for Windows you have to use forward slashes / and not backward slashes \ for the file path. And you can use any file path you want as long as you have system privileges for writing to files at that path location.
Both readFile and writeFile operate in text mode.
Both readFile and writeFile operate in text mode.
B. 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.
Prelude>
C. COMPILE HASKELL PROGRAM
After the GHCi Prelude> prompt type :! ghc --make "*Main" as shown below, then press Enter to compile the program:
[1 of 1] Compiling Main ( Main.hs, Main.o )
Linking Main.exe ...
Prelude>
D. EXECUTE COMPILED HASKELL PROGRAM
Navigate to your working directory. C:/Users/Tinnel/Haskell2024/ is my working directory. From your working directory, 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>
NOTE: You must have sufficient operating system privilege in the directory and folder you want to use to perform this operation! On my computer I used the path C:/Users/Tinnel/Haskell2024/ which is where I have system privilege to write to files.
On Windows 8, from the desktop, hover your mouse over the lower left corner until the start box appears and right click. Select “Command Prompt (Admin)”.
As shown below, after the > prompt type Main.exe and press Enter and the compiled program will be executed:
C:/Users/Tinnel/Haskell2024>Main.exe -- Writes to file not the screen!
C:/Users/Tinnel/Haskell2024>
E. READ FILE
From your text editor, open C:/Users/Tinnel/Haskell2024/txtFileOut.txt and you will see the following:
A hearse. A hearse. My kingdom for a hearse.
Congratulations! You have written to a file!
II. CONCLUSION
In this tutorial, you have received an introduction to writing to a file.
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.