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.

Introduction

image

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

About Haskell

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.

clip_image001

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.

clip_image003

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.

clip_image005

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.

clip_image007

Haskelly offers is text-hovering, which can show additional information such as function type definitions.

clip_image009

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.

clip_image011

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.

clip_image013

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.

clip_image015

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.

clip_image016

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)

3. Install Stack – For Windows users such as myself, you would need to use the installer found here, otherwise follow the instruction for your Operating System found here

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

Comments (0)

Skip to main content