Learning Haskell and using VSCode and Haskelly
This is a guest post by Nicholas Li, Undergraduate MEng student at Imperial College London and Microsoft Student Partner.
I am Nick, and I grew up and studied in Hong Kong. I am currently studying in Computing at Imperial as a first year. Aside from Computing, my passions include Basketball, Badminton, as well as Television shows
Haskell, a functional programming language, is a great introduction to programming due to its similarity to maths in basic functions, as well as its simplicity to run and compile.
For example, the following code will read a list of integers from command line, then double each element of the list, and print it out.
Haskell (Left) is clearly easier to learn for a beginner, and even the more complex list comprehension version (Line 4) is easier than the 23-line long Java (Right) method. Of course, Java has its benefits, but as a beginner, it is much easier to pick up Haskell than Java.
VSCode & Haskelly
Many of us were taught to code in a text editor like Atom for Haskell, and use command line to compile and run our program. Basic text editors lack functions such as text suggestion, Git support, and many other features. Visual Studio Code, along with Haskelly, a Haskell extension has all the listed features, and many more as highlighted below.
To test VScode, I decided to redo one of my past papers with it.
To begin simply press (Ctrl-Shift-P) to bring up the command palette, where you can search for the many features that VScode offers.
Typing in “git clone” will allow you to paste in a git repository URL, and download it to your desired location.
There are many other uses of this command palette, and you can type “?” in it to find more, or type in relevant key words to find what you’re looking for.
VScode has many features that your average text-editor would not have. For instance, highlighting a section of your code would also highlight all the other instances where this exact line of code has come up, and pressing “Ctrl-F2” would allow for refactoring all the instances. This allows for easy simplification of duplicated code.
Haskelly offers is text-hovering, which can show additional information such as function type definitions.
Haskelly, and VScode together also offers IDE-like features such as Intelligent Code Completion, and allows for an easier transition when you begin to learn languages such as Java, where most choose to rely on an IDE.
If I was introduced to VScode along with Haskelly when I first began learning Haskell, I would’ve chosen VScode over a standard text editor. For a beginner, having features like text-hovering to show the type information rather than typing “:t” in a command prompt would’ve saved a lot of time. Furthermore, having a built in terminal, and being able to simply press ‘Load GHCi’ rather than having to type switch windows, then type in the command makes the whole process more fluid.
The Haskelly extension also includes Haskell QuickCheck, which is a simple way of running extensive tests for your functions. Combining your knowledge from your logic course with programming, you would need to be able to deduce a property that your function will satisfy after it returns a result, also known as a post condition.
For example, this function should reverse any given integer list.
Reverse is an involutory function, meaning that reverse (reverse x) = x. The following property should be satisfied.
myReverse (myReverse xs) == xs
To be sure, we can use the QuickCheck button on the bottom left, along with a function that begins with “prop_” which takes in some arguments, and returns a Boolean. This is needed for QuickCheck to know which function to test.
For a video demo on Haskelly and VScode in action
There are a few extra steps you must do to use Haskelly with VScode compared to using a text-editor.
1. Download VScode from here
2. Download the Haskelly extension by typing in “ext install haskelly” into QuickOpen (Ctrl + P)
4. Install Intero (Intelligent Code Completion) and QuickCheck (TestSuite) by typing the following into command prompt:
stack install intero QuickCheck stack-run
stack build intero QuickCheck stack-run