REVISED: Friday, November 15, 2024
I. HASKELL INTRODUCTION
Haskell has its roots in academia. The language is named after Haskell Brooks Curry whose research into mathematical logic laid the foundation for functional languages. Haskell is based on lambda calculus; therefore, the Greek letter λ pronounced ˈlæm.də has been used as the Haskell logo.
Haskell uses function calls and is; therefore, referred to as functional programming. A functional language program uses functions to define the data, and when executed by the computer, the functions answer questions regarding the data.
C and C++ use imperative programming which is a sequence of steps of instructions executed by the computer to answer questions regarding the data.
Unlike imperative languages, computations in Haskell are done by declaring functions which describe what something is instead of declaring instructions explaining how to do the computations. When we program in Haskell we are evaluating function expressions rather than executing instructions.
However, Haskell does have the ability to accomplish the execution of sequences of steps of instructions by using "do" notation. In fact Haskell accomplishes the execution of sequences of steps of instructions so well that quoting Simon Peyton Jones, "Haskell is the world’s finest imperative programming language."
Haskell uses function calls and is; therefore, referred to as functional programming. A functional language program uses functions to define the data, and when executed by the computer, the functions answer questions regarding the data.
C and C++ use imperative programming which is a sequence of steps of instructions executed by the computer to answer questions regarding the data.
Unlike imperative languages, computations in Haskell are done by declaring functions which describe what something is instead of declaring instructions explaining how to do the computations. When we program in Haskell we are evaluating function expressions rather than executing instructions.
However, Haskell does have the ability to accomplish the execution of sequences of steps of instructions by using "do" notation. In fact Haskell accomplishes the execution of sequences of steps of instructions so well that quoting Simon Peyton Jones, "Haskell is the world’s finest imperative programming language."
Concurrency and parallelism are the future of computer programming. Haskell has both concurrency and parallelism. Therefore, flexible, high-quality, maintainable software is easy to produce with Haskell.
Haskell types do not have to be declared by a programmer; however, all types used in a Haskell program are checked by the Haskell compiler. Also, Haskell is a lazy, nothing is done until it needs to be, language. Therefore, Haskell programmers can write more concise Haskell functions that include working with infinity.
Haskell is an open-source product of more than thirty-seven years of research which began in 1987. Haskell programmers produce accurate marketable software more quickly than their imperative programming counterparts. Haskell provides built-in concurrency, parallelism, and integration support for its user community, other languages, debuggers, profilers, and third party libraries.
Haskell is an open-source product of more than thirty-seven years of research which began in 1987. Haskell programmers produce accurate marketable software more quickly than their imperative programming counterparts. Haskell provides built-in concurrency, parallelism, and integration support for its user community, other languages, debuggers, profilers, and third party libraries.
Haskell is not an object oriented language, it is; however, a more abstract style of programming. Haskell functional programming is based upon the application of functions to arguments. Haskell is free, open source and community driven. Haskell is available for major operating systems including Windows, Mac OS X, and Linux. In summary Haskell is a pure, functional, lazy, type-safe, algebraic, inferential, and parallel programming language.
II. DOWNLOADING AND INSTALLING HASKELL
Downloading and Installing Haskell has changed from Chocalatey to GHCup. As of 2022 the Haskell Platform was deprecated and is no longer the recommended way of installing Haskell. You now install GHC, cabal-install and haskell-language-server via GHCup ; and you install Stack, by following the Stack installation guide at Haskellstack . Use the following video as a guide for installing Haskell on Windows . Now instead of seeing "Haskell Platform" you will see CHCup or "Haskell Toolchain" being used to compile, interpret, and review Haskell programs. The Haskell Toolchain or CHCup consists of ghc, runghc, and ghci. The Haskell Platform used to ship with the WinGHCi interpreter, a GUI version of the GHCi command line interpreter. However, the WinGHCi interpreter is not included with the current update of the Haskell Toolchain (ghc, ghci and cabal) or CHCup. Therefore, I now use Visual Studio Code as my editor instead of WinGHCi.
The GHCI 9.8.1 release is the recommended download. GHCI 9.8.1 includes a Glasgow Haskell Compiler (ghc), a Glasgow Haskell Interpreter (ghci), a Cabal build system, and the Stack Tool for controlling projects.
If you are new to Haskell, I recommend starting with the GHCI 9.8.1 release. It is the most stable and feature-rich release of the compiler, and it will give you the best experience when learning Haskell.
The install directory must be included in your system PATH variable. The Windows installer should have done this for you. You can confirm this; depending on your operating system, go to your control panel and select system.
Next, select advanced system settings. Then select environmental variables. Next, select system variables, then select edit, and then select path. On the path line you should see:
C:\ghcup\ghc\9.8.1\bin\ghci-9.8.1.exe
If not, move your cursor to the very end of the path line, type a semicolon ; followed by a space and then type
C:\ghcup\ghc\9.8.1\bin\ghci-9.8.1.exe
and then keep pressing OK until you exit out of the control panel. If you are using Windows 10 the steps are slightly different. Windows 10 provides a list of each system variable and if the one shown above is not listed you edit the list by adding it to the list as a new item.
C:\ghcup\ghc\9.8.1\bin\ghci-9.8.1.exe
and then keep pressing OK until you exit out of the control panel. If you are using Windows 10 the steps are slightly different. Windows 10 provides a list of each system variable and if the one shown above is not listed you edit the list by adding it to the list as a new item.
III. HASKELL DOWNLOAD AND INSTALLATION VALIDATION
You should create a Haskell shortcut icon on your desktop to make it easy to start Haskell. There are many ways to make a shortcut depending on your operating system. I went to C:\ghcup\ghc\9.8.1\bin\ghci-9.8.1.exe and selected ghci-9.8.1.exe then right mouse clicked and selected create shortcut. After the shortcut was created I did an "Edit, cut" and cut the shortcut and pasted it to my screen so it would be on my desktop whenever I needed it. If you are using Windows 10 you will not have to do the "edit cut past," you will be prompted for the final location of the shortcut icon and you can select desktop.
Double left mouse click the Haskell icon on your desktop to start Haskell. The Haskell ghci-9.8.1.exe window will open, and either the Prelude> prompt or the ghci prompt will appear.
When you see the Haskell window and either of the above prompts you have successfully downloaded and installed Haskell.
As shown below type :? after either prompt then press ENTER to see a list of available commands.
Prelude> :?
Commands available from the prompt:
<statement> evaluate/run <statement>
: repeat last command
:{\n ..lines.. \n:}\n multiline command
:add [*]<module> ... add module(s) to the current target set
:browse[!] [[*]<mod>] display the names defined by module <mod>
(!: more details; *: all top-level names)
:cd <dir> change directory to <dir>
:cmd <expr> run the commands returned by <expr>::IO String
:complete <dom> [<rng>] <s> list completions for partial input string
:ctags[!] [<file>] create tags file for Vi (default: "tags")
(!: use regex instead of line number):def <cmd> <expr> define a command :<cmd> (later defined command
has precedence, ::<cmd> is always a built in
command)
command)
:edit <file> edit file
:edit edit last module:etags [<file>] create tags file for Emacs (default: "TAGS")
:help, :? display this list of commands
:info [<name> ...] display information about the given names
(!: do not filter instances)
:issafe [<mod>] display safe Haskell information of module <mod>
:kind <type> show the kind of <type>(!: also print the normalized type)
:load [*]<module> ... load module(s) and their dependents
:main [<arguments> ...] run the main function with the given arguments
:module [+/-] [*]<mod> ... set the context for expression evaluation
:quit exit GHCi
:reload reload the current module set
:run function [<arguments> ...] run the function with the given arguments
:script <filename> run the script <filename>
:type <expr> show the type of <expr>
:undef <cmd> undefined user-defined command :<cmd>
:!<command> run the shell command <command>
-- Commands for debugging:
:abandon at a breakpoint, abandon current computation
:back go back in the history (after :trace)
:break [<mod>] <l> [<col>] set a breakpoint at the specified location
:break <name> set a breakpoint on the specified function
:continue resume after a breakpoint
:delete <number> delete the specified breakpoint
:delete * delete all breakpoints
:force <expr> print <expr>, forcing unevaluated parts
:forward go forward in the history (after :back)
:history [<n>] after :trace, show the execution history
:list show the source code around current breakpoint
:list identifier show the source code for <identifier>
:list [<module>] <line> show the source code around line number <line>
:print [<name> ...] prints a value without forcing its computation
:sprint [<name> ...] simplified version of :print
:step single-step after stopping at a breakpoint
:step <expr> single-step into <expr>
:steplocal single-step within the current top-level binding
:stepmodule single-step restricted to the current module
:trace trace after stopping at a breakpoint
:trace <expr> evaluate <expr> with tracing on (see :history)
-- Commands for changing settings:
:set <option> ... set options
:seti <option> ... set options for interactive evaluation only
:set args <arg> ... set the arguments returned by System.getArgs
:set prog <progname> set the value returned by System.getProgName:set prompt <prompt> set the prompt used in GHCi
:set prompt2 <prompt> set the continuation prompt used in GHCi
:set editor <cmd> set the command used for :edit
:set stop [<n>] <cmd> set the command to run when a breakpoint is hit:unset <option> ... unset options
Options for ':set' and ':unset':
+m allow multiline commands
+r "revert top-level expressions after each evaluation"
+s print timing/memory stats after each evaluation
+t print type after evaluation
-<flags> most GHC command line flags can also be set here
(eg. -v2, -XFlexibleInstances, etc.)
for GHCi-specific flags, see User's Guide,
"Flag reference, Interactive-mode options"
-- Commands for displaying information:
:show bindings show the current bindings made at the prompt
:show breaks show the active breakpoints
:show context show the breakpoint context
:show imports show the current imports
:show linker show current linker state
:show modules show the currently loaded modules
:show packages show the currently active package flags:show paths show the currently active search paths
:show languages show the currently active language flags
:show <setting> show value of <setting>, which is one of[args, prog, prompt, editor, stop]
IV. COMMONLY USED ABBREVIATIONS
:i for :info
:k for :kind
:l for :load
:q for :quit
:r for :reload
:t for :type
V. GHCi PROMPT
The Prelude> prompt displayed by GHCi can often grow very long depending on the modules loaded. If the prompt's length becomes a problem you can change the prompt by using the following command:
GHCi>
In the following tutorials, I will use both prompts occasionally.
VI. GHCi OPTIONS
Check to see what options came with your GHCi download as follows:
Prelude> :show
options currently set: +t
base language is: Haskell2010
with the following modifiers:
-XNoDatatypeContexts
-XNondecreasingIndentation
GHCi-specific dynamic flag settings:
other dynamic, non-language, flag settings:
-fimplicit-import-qualified
warning settings:
Prelude>
Notice I have changed my copy of Haskell so the first line shows options currently set: +t and the last line shows warning settings:. I wanted you to be able to see the type as you read the tutorial. Your copy of Haskell will start with options currently set: none.
In Prelude> :? above, you see in Options for ':set' and ':unset': that +t will print type after evaluation:
Prelude> let x = 2 + 2
x :: Num a => a -- The +t tells GHCi to print the "type" after the evaluation.
Prelude>
Prelude> x
4
it :: Num a => a -- The +t tells GHCi to print the "type" after the evaluation.
Prelude>
Prelude> :set +t -- Turns +t on.
Prelude>
Prelude> :unset +t -- Turns +t off.
Prelude>
x :: Num a => a -- The +t tells GHCi to print the "type" after the evaluation.
Prelude>
Prelude> x
4
it :: Num a => a -- The +t tells GHCi to print the "type" after the evaluation.
Prelude>
Prelude> :set +t -- Turns +t on.
Prelude>
Prelude> :unset +t -- Turns +t off.
Prelude>
The "Haskell Functions Introduction" tutorial will explain "type."
VII. HASKELL UPDATES
The Haskell language has evolved significantly since its birth in 1987. A new major GHCup release with library extensions and new tools appears every six months. Minor GHCup releases, with bug fixes, occur during each six month cycle.
When you update an older GHCup version, you will normally be prompted during the update process, to first uninstall your older version of GHCup.
If you are using Windows 10 to uninstall GHCup, right mouse click the Windows key in the lower left corner of the screen and a selection window will appear. At the top of the selection window is a search window. Type "file explorer" or "control panel" in the search window and press enter. A file explorer or control panel window will open. The C:\ghcup folder is the one you are looking for. Left mouse click GHCup then right mouse click uninstall and follow the prompts that appear to uninstall the old GHCup. Depending on your system you want either file explorer or the control panel whichever one that lets you do an uninstall and not a delete.
If you are using Windows 10 to uninstall GHCup, right mouse click the Windows key in the lower left corner of the screen and a selection window will appear. At the top of the selection window is a search window. Type "file explorer" or "control panel" in the search window and press enter. A file explorer or control panel window will open. The C:\ghcup folder is the one you are looking for. Left mouse click GHCup then right mouse click uninstall and follow the prompts that appear to uninstall the old GHCup. Depending on your system you want either file explorer or the control panel whichever one that lets you do an uninstall and not a delete.
At present GHCup updates do not include backwards compatibility functionality. Therefore, when you update your copy of the GHCup, you probably will want to also run and update all of your Haskell code to conform to the new GHCup version.
This tutorial series was originally written using Haskell Platform 2014.2.0.0 for Windows. I am in the process of updating the complete tutorial to GHCup ghci-9.8.1 for Windows.
This tutorial series was originally written using Haskell Platform 2014.2.0.0 for Windows. I am in the process of updating the complete tutorial to GHCup ghci-9.8.1 for Windows.
VIII. PROPORTIONALLY SPACED FONT
Most software developers prefer to use a monospaced font because spaces and all characters have the same width; therefore, all your columns line up nicely. However, at present Google Blogger uses proportionally spaced font. Therefore, after the first non-space on a line, columns will not line up any more, because characters like "i" and "m" have different widths. As far as I know, Google Blogger does not allow any tag that gives fixed width text. That is why all the columns and figures in this tutorial look unusual and are not drawn to scale. However, if you know how to fix this problem leave me a comment with the answer and I will change my posts.
IX. WHITESPACE, JUXTAPOSITION, OR INDENTATION, ETC.
Working through my tutorials will involve using your text editor to copy paste example Haskell programs. All of the examples in the tutorials have been tested and they all work. If you have problems check the whitespace, juxtaposition, indentation, etc., in your copy. Sometimes you will not get an exact copy and it will prevent your program from working. Your copy should have whitespace formatted as follows:
Code which is part of some expression should be indented further in than the beginning of that expression.
Keep lines to no more than 80 columns.
Do not use tabs, use spaces.The "in" keyword is usually aligned directly under the "let" keyword.
Use CamelCase, do not use underscores, for variable names.
Do not leave trailing white space in your lines of code.
Do not use braces and semicolons.
Use spaces for indenting.
Indent code blocks 4 spaces. Blocks start with "let", "where", "do" and "of."
Indent the "where" keyword 2 spaces.
Indent the "where" definitions 2 spaces.
Indent "guards" 2 spaces.
-}
X. VISUAL STUDIO CODE EDITOR
Download and install the Visual Studio Code (VSC) editor.
The top left corner of the VSC screen has two toolbars; the one called "Menu" is horizontal, and the other called "Activity Bar" is vertical.
Select "Help" in the horizontal Menu toolbar. When the window under Help opens select "Get Started." Under the "Walkthroughs" column select "Learn the Fundamentals." Scroll down Learn the Fundamentals, select "Lean Back and Learn" and watch all of the tutorials.
Select "Extensions" from the left vertical Activity toolbar on VSC. A search window will open. Use this window to find, download, and install "Haskell" and "Haskell Syntax Highlighting."
When you are ready to start programming in Haskell select "File" on the top left horizontal Menu toolbar; then select "New Text File." A window will open in the top center of the screen and it will ask you to "Select a Language or open a different Editor to get started." Left-click this request, scroll down, and select "Haskell." Start typing the following sample Haskell Module program.
-- Pet.hs
module Pet where
main :: IO ()
main = do
putStrLn "What is your favorite pet? "
pet <- getLine
putStrLn $ "Amazing! My favorite pet is also a " ++ pet ++ "!"
When you are ready to save your program select "File" on the top left horizontal Menu toolbar; then scroll down and select "Save As." The next window will be similar to Explorer which you will use to find the folder where you want your file saved. If necessary create a new folder. You will see a placeholder file name at the bottom left of the window. Change the placeholder to Pet.hs then press Enter.
When you are typing a Haskell Module the Module name and the file name must be the same, and they must both start with a capital letter. Your file will be created in the folder you selected and you will be moved to a VSC work area in the top center of VSC which you will use to write your Haskell program.
When you are ready to debug and run your program use Microsoft Powershell
by selecting "Terminal" on the top left horizontal Menu toolbar on VSC and
a response similar to the following will appear in the bottom center of VSC:
PowerShell 7.3.1
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS C:\Users\Tinnel>
VSC is asking what do you want to work with. You want "ghci" so after the " > " prompt type ghci and then press Enter.
PS C:\Users\Tinnel>ghci
VSC will respond as follows:
PS C:\Users\Tinnel> ghci
GHCi, version 9.8.1: https://www.haskell.org/ghc/ :? for help
Prelude>
The Prelude> prompt means you are in GHCi. GHCi is asking for the path to your "working directory", in other words, the directory containing the module you want to load and work with. Therefore, you will type something similar to the following:
GHCi, version 9.8.1: https://www.haskell.org/ghc/ :? for help
Prelude> :cd c:\users\tinnel\haskell2024\newProjects\
Prelude>
GHCi is asking you to load the module in this working directory; therefore, you would type something similar to the following:
Prelude> :load Pet.hs
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, one module loaded.
*Main>
You have loaded the following Haskell program example into GHCi:
-- Pet.hs
module Pet where
main :: IO ()
main = do
putStrLn "What is your favorite pet? "
pet <- getLine
putStrLn $ "Amazing! My favorite pet is also a " ++ pet ++ "!"
GHCi is asking you to type "main" to start the module main.
*Main> main
What is your favorite pet?
You answer the question.
dog
And GHCi responds
Amazing! My favorite pet is also a dog!
*Main>
To exit GHCi type :quit after the > prompt and press Enter.
*Main> :quit
Leaving GHCi.
PS C:\Users\Tinnel>
To exit PS and VSC select File in the top left horizontal Menu tool bar of VSC, scroll down and press Exit.
Now you are ready to enjoy the wonders of Haskell. The following tutorials will explain how to write, debug, and compile Haskell programs.
XI. CONCLUSION
In this tutorial, you have begun to learn how to download, install and use the Haskell programming language, the GHCi interpreter, and the Visual Studio Code editor. The following tutorials listed from the bottom to the top in the "Blog Archive" located in the top right corner of this tutorial will provide more detailed information on Haskell topics to help you enjoy the wonders of Haskell! As you work through the tutorials if you have questions or comments you can contact me at rtinnel@insight.rr.com and a complete list of my blog posts is listed at https://elcricottocircle.blogspot.com/ I am looking forward to helping you.
As you have probably guessed these tutorials are notes to myself to help me remember everything I have learned about functional computer programming. I offer all of my tutorial notes to you free of charge, a gift that I hope you enjoy and benefit from as much or more than I have.
XII. REFERENCES
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.