Monday, December 31, 2012

HASKELL GHCI "INTERACTIVE MODE"

HASKELL GHCI "INTERACTIVE MODE"




REVISED: Friday, February 9, 2024





Haskell GHCi interpreter interactive mode.


I. HASKELL GHCI INTERPRETER

We have two options for working with the GHCi interpreter.  First, we can use your favorite  editor or the Visual Studio Code editor. Second,  we can work with the GHCi interpreter without an editor.

A. VISUAL STUDIO CODE (VSC)

If you choose to use the Visual Studio Code editor, double left mouse click the Visual Studio Code icon on your desktop. If you followed along with VSC in the previous "Downloading And Installing Haskell" tutorial you will probably remember VSC will open with a horizontally split window showing the Main.hs program at the top and the Power Shell (PS) prompt at the bottom.

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>

When you see the Prelude> prompt you know you are in GHCi.

First, you must tell GHCi the path to your "working directory" as follows:

GHCi, version 9.8.1: https://www.haskell.org/ghc/  :? for help
Prelude> :cd c:\users\tinnel\haskell2024\newProjects\
Prelude>

Second, GHCi is asking you to load the module located in this working directory. Therefore, you would create a new module file; e.g., Second.hs by following the directions described in the previous "Downloading And Installing Haskell" tutorial.  Then based on your module name, type something similar to the following:

Prelude> :load Mian.hs
[1 of 1] Compiling Main             ( Main.hs, interpreted )
Ok, one module loaded.
*Main> 

B. GHCi

As I said before, you have two options for working with GHCi. You can write Haskell programs using an editor; e.g., the VSC editor or you can work directly with GHCi.

Double left click the Haskell shortcut icon you created on your desktop in the previous "Downloading And Installing Haskell" tutorial and the following will appear.

GHCi, version 9.8.1: https://www.haskell.org/ghc/  :? for help
Prelude>

The Prelude> prompt means you are in GHCi.

In the following tutorials if I do not say what method is being used to work with GHCi we are not working with the VSC editor, we are working directly with the GHCi interpreter without an editor.

C. GHCI INTRODUCTION

GHCi is an interactive Haskell Read-Eval-Print-Loop (REPL) that comes with GHC. The Haskell GHCi interactive interpreter accepts your user input statements after the Prelude> prompt.

Haskell has referential transparency. Referentially transparent means no mutation, which means variables, data structures, everything within a particular program, is immutable or in other words unchanging over time, or unable to be changed.

In Haskell you never assign a variable, instead you bind a name to a value. All variables you use are statically bound. Therefore, you can bind functions and values to names, and use those names in statements or expressions.

D. LET

Long GHCi commands, for example input functions such as an implicit do block, often cover more than one line of code. To communicate multiple line commands to GHCi, wrap them between :{ and :}.

After the Prelude> prompt type :{ then press Enter. GHCi will respond on the next line with Prelude| after which you type your first line of Haskell code ending with pressing Enter. When you are done entering your program type :} on a line by itself then press Enter and GHCi will run your program and print the program results on the next line.

There are many ways to program an implicit do block in Haskell. One bare bones approach, is shown below. For this to work it should be hand typed, "copy paste" will result in an error message unknown command ':{'.

Prelude> :{
Prelude| let x = 12
Prelude|      y = 5
Prelude| in  x * y
Prelude| :}
60
it :: Num a => a
Prelude>  

You let a pattern or name be "equal to" or "defined as" variables in an expression.

The syntax is let <bindings>  in <expression>. The variables defined in the let expression can be used by the in expression. let begins with bindings and is followed by an expression. First x is bound to 12 and y is bound to 5. Then x is multiplied by yHaskell requires that each variable must be bound to a value.

When you are typing your Haskell program and you need white space, do not type tabs, only type spaces. As shown below indentation counts.  The x is the first name in the let expression. The y and all subsequent names in the let expression must be indented to line up with the first name in the let expression.

The let expression in the example below does not line up the x and y names in the let expression and this creates a parse error.

Prelude> :{
Prelude| let x = 12
Prelude| y = 5
Prelude| in x * y
Prelude| :}

<interactive>:23:1: parse error on input `y'
Prelude>

Never use in in a list comprehension or a non-implicit do block.

let is used three ways in Haskell.

First, as a let expression.

let name = expression in expression

A let must be used wherever an expression is used. For example, use let to define a local variable scoped over an expression:

Prelude> (let x = 0 in x^0) + 1
2
it :: Num a => a
Prelude>  

Use the ^ exponent operator to use an integer as the exponent.

Use the ** exponent operator to use a floating point number as the exponent. 
  
Second, as a let statement, only inside do notation, without using in.

do statements
       let name = expression
       statements

Third, used inside of list comprehensions, without using in.

Prelude> [(x, y) | x <- [5..12], let y = 12*x]
[(5,60),(6,72),(7,84),(8,96),(9,108),(10,120),(11,132),(12,144)]
it :: (Num t, Enum t) => [(t, t)]
Prelude>

E. USING DO

Always remember long GHCi commands, as shown below, can cover more than one line when wrapped in :{ and :}.

Prelude> :{                                                        -- Must be on line by itself.
Prelude| let prRev = do {                                      -- Starts prRev function declaration.
Prelude|        inp <- getLine;                                 -- Indentation counts!
Prelude|        putStrLn $ reverse inp;                 -- Vertical alignment is a must!
Prelude| }                                              -- Ends function declaration.
Prelude| :}                                                               -- Must be on line by itself.
prRev :: IO ()
Prelude>

Prelude>prRev                                                 -- prRev function call then press Enter
Hello, World!                                                          -- Type getLine input then press Enter
!dlroW ,olleH                                                          -- prRev reverses getLine input
it :: ()
Prelude>

F. WORKING DIRECTORY

The "working directory" is the "current directory," in other words the folder that is currently open.

For example my tutorial working directory is:

C:\Users\Tinnel\Haskell2024\newProjects\

I create a new working directory for new Haskell tutorial files each year.

I change to the working directory in GHCi by using the following commands:

Prelude> :cd C:\Users\Tinnel\Haskell2024\newProjects\
Warning: changing directory causes all loaded modules to be unloaded,
because the search path has changed.
Prelude>

Then I use the following to check to make sure the path was changed correctly: 

Prelude> :show paths
current working directory:
     C:\Users\Tinnel\Haskell2024\newProjects
module import search paths:
Prelude>

II. SAVING USER DEFINED HASKELL FUNCTIONS

Create the following file in your text editor and save it as fun1.hs:

-- My first Haskell function!
doubleX x = x + x

Notice the fun1.hs "file type" extension is hs; and there has to be a period . between the "file name" and the "file type." The file you just created can also be referred to as a "Haskell script."

Next use the :load command, as shown below, to load the Haskell script into GHCi:

Prelude> :load fun1
[1 of 1] Compiling Main             ( fun1.hs, interpreted )
Ok, 1 module loaded.
*Main>   

Notice ghci uses Main; however, our script fun1 does not contain a Main.  A Haskell program needs to have an “entry point” called main.  If your script does not contain a Main ghci will create one as it has for our script fun1.

We will call our function doubleX and have it process the number 2 as follows:

*Main>  doubleX 2
4
it :: Num a => a
*Main>

(The it :: Num a => a line of code is a "function signature." Function signatures will be discussed in detail in the "Haskell Functions Introduction" tutorial.)

Congratulations, you have just written your first Haskell function!

III. HASKELL INTERACTIVE FEEDBACK LOOP

WinGHCi was a GUI to run GHCi on Windows. Currently, there is no installation program for WinGHCi.  To start Haskell double left mouse click the Haskell icon you created on your desktop.  A window similar to the following will open:

GHCi, version 9.8.1: https://www.haskell.org/ghc/  :? for help
Prelude> 

Notice you have selected GHCi and the window you opened has a black background and the title GHCi. Before the update Windows users could select WinGHCi and open a window with a white background and the title WinGHCi at the top. Unless I find an installation program for WinGHCi I will be using GHCi throughout this tutorial series. 

Haskell code can be run interactively, or compiled to an executable. The runtime system interprets the byte-code created when the Haskell source code is processed by GHCi. 

The Haskell program we created above contains the Haskell function doubleX. Haskell, functions are called by writing the function name, a space, and then the arguments, separated by spaces. 

After the Prelude> prompt, type either :load or  :l (colon followed by the lower case letter "l") followed by the file name fun1, and then press Enter. After your input you should see the following response.

Prelude> :load fun1
[1 of 1] Compiling Main             ( fun1.hs, interpreted )
Ok, 1 module loaded.
*Main

After the Prelude> prompt, type doubleX 10, and press Enter. After your input you should see the following response.

Prelude> doubleX 10
20
it :: Num a => a
*Main

If you want to change the function, type :edit after the Prelude> prompt and press Enter. The fun1.hs file will be opened by Haskell using the Notepad editor and you can make changes and save the file.

Prelude> :edit
Ok, 1 module loaded.
*Main>

Comments are notes you embed in your program to help you remember why you coded the program the way you did. Start single line comments with "--" (two dashes). Start multiple line comments with "{-" and end them with "-}". Haskell will not print your comments. 

Now you have an interactive feedback loop. You can change functions in the fun1.hs script, and reload the script into Haskell GHCi using :load fun1; or using :r which is equivalent because it reloads the current script.

To exit Haskell GHCi type :q to quit then press Enter.

IV. CONCLUSION

In this tutorial, you have received an introduction to the GHCI interpreter.

V. 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.





DOWNLOADING AND INSTALLING HASKELL

DOWNLOADING AND INSTALLING HASKELL




REVISED: Friday, February 9, 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, we do computations in Haskell 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. Furthermore, flexible, high quality, maintainable software is easier to produce with Haskell.

Haskell types do not have to be declared by a programmer; however, they are all checked by the compiler. Haskell is a lazy, nothing is done until it needs to be, language. Therefore, programmers do not have to be concerned about needing to program functions that work 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 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.

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 the Prelude> prompt will appear.

When you see the Haskell window and the above prompt you have successfully downloaded and installed Haskell.

As shown below type :? after the Prelude> 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)
   :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]
   :showi language               show language flags for interactive evaluation

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:

Prelude> :set prompt GHCi> 
GHCi> 

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>

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.

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.

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 request you to "Select a Language or open a different Editor to get started."  Left click this request, scroll down and select "Haskell."  Start typing your Haskell program. 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 place holder file name at the bottom left of the window. Change the place holder to the file name you want and make sure it has an extension of ".hs" then press Enter. If 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 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" 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 Main.hs
[1 of 1] Compiling Main             ( Main.hs, interpreted )
Ok, one module loaded.
*Main> 

We have loaded the following Haskell program example into GHCi:

module Main 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 Archieve" 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.

XII. 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.