Starting with Haskell

A purely functional programming language

Nishant Aanjaney Jalan
CodeX

--

If you know me personally or have been following my content for a while, you must wonder why I am writing about Haskell — It’s not even used anymore for any practical purposes as of 2022. Well, to be fair, it is going to be taught to me in college, and I decided to get a little headstart to enjoy my freshman parties 🎉.

Source

Introduction

Haskell is a purely functional programming language. It is often used to teach the idea of programming to newbie programmers. One would argue that Java is a better choice than Haskell. I’d agree, but well. . . I do not have an answer to that so moving on.

What is a functional language?

A function is, in layman’s terms, a block of instructions the computer needs to run. If you would compare this with a real-life example, consider a restaurant. A chef’s function is to cook food. Cooking food involves a set of instructions and a workflow — a recipe, if you may. A waiter’s function is to deliver the food from the chef to the customer. Now the customer’s function is to fill his stomach and get going. Everything we deal with here is based on functions. Similarly, Haskell is a functional language that completely relies on executing a set of instructions defined inside of a, yes, a function.

JavaScript is another great example of a functional programming language; however, JavaScript is not purely functional, as recent developments have introduced other topics.

Features of Haskell

The main website of Haskell showcases the features of the programming language.

  1. Statically typed — Haskell will assign a certain type to a variable which shall remain that same type throughout the execution. The type of any variable shall not change in the middle of execution.
  2. Purely functional—As I have not mentioned this a dozen times earlier, everything in Haskell is a function. There are so statements or instructions (perse), only expressions which cannot mutate variables.
  3. Type inference — Unlike Java, you do not have not explicitly mention a type. Haskell will try its best to understand the code and assign types implicitly and wherever applicable.
  4. Concurrent — Haskell is excellent for performing parallel operations on different threads. It has a high-performance garbage collector (also credit to the fact that every variable is immutable) and a lightweight concurrency library.
  5. Lazy — Like you, Haskell does not like to perform operations unless it really needs to (I’m kidding, you’re probably not lazy). Programmatically, functions and variables are not evaluated unless they are invoked.
  6. Packages — Haskell has a big open source contribution and a wide variety of third-party packages hosted online.

Installation

I am a Windows User, and since I already had Chocolatey installed, it was quite straightforward. If you do not have Chocolately installed, I suggest you install that first.

Installing Chocolatey with Powershell

Firstly, Open Powershell as an administrator.

Run the following command:

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Note: The installation takes a long time you can vibe to some of your favourite songs meanwhile.

Installing Haskell

You’re one command away from installing Haskell,

choco install haskell-dev

Hit enter and choose “Y” every time the installation setup asks you to run a script. I hope you are continuing to listen to that music because this installation is about the same length of time.

The GHCI

The basic component installed alongside Haskell is the Glasgow Haskell Compiler (GHC), and this comes with an interactive environment called the GHCi. To start this environment, run ghci in your Windows Powershell and wait for it to load.

Once you have done this, it is very simple to use. Let’s start by defining a variable,

ghci> let num = 5
ghci> num
5
ghci>

You can clear the console by clicking Ctrl+L and you can quit by entering the command, :q, similar to Vim.

I hope you enjoyed reading my article and learnt something. Thank you! ✌️

Want to connect?My GitHub profile.
My Portfolio website.

--

--

Nishant Aanjaney Jalan
CodeX
Editor for

Undergraduate Student | CS and Math Teacher | Android & Full-Stack Developer | Oracle Certified Java Programmer | https://cybercoder-naj.github.io