Haskell, unlike most the more common languages, is a declarative programming language.

what is a declarative programming language
A nice definition from Google so you don’t have to Google it yourself…

So, the way we write programs in haskell is a tad different to what you’re probably used to if you have any experience..

Once you’ve got your haskell compiler (I use GHCI) installed and running, then you’ll want to write your first haskell program.

If you don’t have haskell yet then there’s an easy solution for you.

How To Install GHCI, Haskell, The Easy Way

I had a quite an issue with this when I first started out.

GHCI is the main Haskell Compiler that we’re told to use in our lectures, YouTube videos and other educational resources, but actually installing it can be a paid in the arse.

Don’t worry though, just don’t go to the GHCI website.

There’s a lovely precompiled installer that you can use through Haskell Platform.

haskell platform download page
Click to download, if you want to.

Writing Your First Haskell Program

So lets get onto some simple Math programs in Haskell.

Addition Function

addFiveToThis :: Int -> Int
addFiveToThis numberInput = numberInput + 5

Line 1, denotes the name of the function along with the Int input and the Int output.

Line 2, tells the program what to do.

In this case, I’ve declared that:

  • addFiveToThis takes in some Int(eger) “n”,
  • and outputs, as an Int(eger), “n” + 5.

Comments, Multiplication, Division & Subtraction Functions

-- this is a comment, it's not run by haskell

-- this is a simple multiply by 5 function
multiplyBy5 :: Int -> Int
muliplyBy5 number = number * 5

-- this is a "curried function" that takes in two integers
divideFirstByLast :: Int -> Int -> Int
divide first last = first / last

-- this is another function, that subtracts 900 by default
subtract900 :: Int -> Int
subtract number = number - 900

The “–” denotes a comment.

You can also see that the “divideFirstByLast” function has an extra “Int ->”.

See this as simply saying we have 2 inputs, that will end up with an Int; in this case we have “first” (an Int) and “last” (an int).

Saving Your Haskell Program to a File

Presuming you’ve got your code ready in some text editor(I advise Sublime Text), you need to save your text file as a “.hs” file.

Running Your Haskell Program

When you double click on your saved file, after you’ve install Haskell Platform, a black command prompt screen will show.

If all the modules have loaded okay, then you know there’s no errors, and you can type in your function with some input and expect your output!

A cool trick you can use to save yourself some typing hassle is to start typing the function want to use, then press the Tab button to auto-complete your function name.

Break It On Purpose So You Get Used to The Errors

I’ve added quotes around the 5 to make it a String type, now we’ll run it.

The :reload command is really good at making this quick and easy for you.

After saving the newly broken file, I can see the exact reason for the error, the line it’s on and what caused the error too.

This is useful, don’t be afraid of them, read them.


Going to have a tonne more coming out soon, keep up if you’re revising for Haskell!

Until next time, Josh.

Leave a Reply
You May Also Like

Java Scanner: How to Read Text Files in Java

The Scanner is super useful for taking user inputs. However, you might…

Databases & Database Management Systems: Introduction

Databases are used everywhere. Facebook, YouTube, your local library airport check-in desks,…

eBay Arbitrage: How to Make Money with eBay Retail Arbitrage

What is a “Just-in Time” eBay Business A business that gets there…

Using IFTTT to automate a simple money-making process

If you like the thought of making money online with little work then…