The blog post outlines how I set up my environment to run Haskell code in VSCode.

Before diving into the code, you will need to set up your environment first. There are multiple ways to set it up, but I would like to highlight how I did it on my device (M2 MacBook).
Install GHCup
GHCup is the main installer that makes it easier to install GHC, GHCi, Cabal, and other useful dependencies for Haskell.
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
You can put the above script in your terminal to install GHCup. When you run this, you will be asked three questions before the installation. The following is how I answered:
Do you want ghcup to automatically add the required PATH variable to "User/YOUR_USERNAME/.bashrc"? => A (Yes, append)
Do you want to install haskell-language-server (HLS)? => Y (Yes)
Do you want to install stack? => N (No)
I decided not to install stack because it is more for larger projects. Once you answer all the above questions and finish installing GHCup, you can either restart your terminal or run . /USERS/YOUR_USERNAME/.ghcup/env to use the commands. Before moving to the next step, try using ghcup tui to see if the installation was successful. (You can quit GHCup by typing q.)
Set Up VSCode
If you don't have VSCode installed, go to this link and set it up. This is many developers' favorite IDE, with countless useful features and extensions. (Use Dark Mode for your eyes.) In VSCode, open the Extensions page and type "Haskell" in the search box. At the top, there is an official extension named "Haskell," so install and enable that extension. Upon installing, there might be a few questions regarding configurations, but select the default answers and you should be good to go. (If you have an icon theme in your VSCode, I recommend disabling it in your workspace for Haskell, because I personally love the default icon for .hs files.)
Also, open the Extensions page again and type "code runner" in the search box. You will find a extension
"Code Runner" by Jun Han, so make sure you install and enable that extension as well. This will come in
handy for executing and debugging your code. Once you installed Code Runner, I would highly recommend
going to settings of Code Runner extension and turn on "Run on Terminal". This can execute a function named main
.
Write Your First Haskell Code
To test if you have configured the environment correctly, let's write a simple code and run it with GHCi. Create a file called demo.hs in VSCode, and type the following:
main = putStrLn "Hello World!"
If you write the above, you should see main :: IO () just above your code. That is the inferred type of the function. Click it or type main :: IO () to have the following code in your file:
main :: IO ()
main = putStrLn "Hello World!"
The above code simply prints "Hello, World!" in your terminal. (Don't worry about the syntax and details for now.) Now, open your terminal in VSCode (Terminal -> New Terminal), and type the following command:
ghci demo.hs
You should see something like this in your terminal:
GHCi, version #.#.#: https://www.haskell.org/ghc/ :? for help
[1 of 2] Compiling Main ( demo.hs, interpreted )
Ok, one module loaded.
ghci>
Here, type main
. You should then see the following:
ghci> main
Hello World!
Congratulations! You have successfully set up your environment and run your first Haskell program!
Alternatively, you can click play button at the top right corner to run main
. (When using this, you can
only run function named main
.)
Making Changes
If you want to change your code, let's say to add your name instead of "World," first rewrite it in demo.hs.
main :: IO ()
main = putStrLn "Hello TK!" -- Changed from "World" to "TK"
Hit Command+S to save the changes to the file. (I forget this a lot of times.) OK, try running main again in your GHCi. What happened? It should still print "Hello, World!" instead of "Hello, YOUR_NAME!". Why? It is because GHCi needs to reload the file. To reload the file, do the following:
ghci> :l demo.hs
ghci> main
Hello TK!
Now, it should print your name correctly. If you want to exit GHCi, hit Control+D. Congratulations! You are all set to embark on the journey to becoming a Haskeller!