Getting Started with Haskell

Getting Started with Haskell

If you are interested in learning a programming language for systems programming, dev tools engineering, or even blockchain development, one of the best languages available is Haskell programming language. In this article, you will learn about Haskell, how to set it up on your computer, and what the syntax looks like. You will also find resources to get started learning Haskell properly.

What is Haskell

Haskell is a statically typed, purely functional programming language that was first developed in the late 1980s by a group of researchers led by Professor Simon Peyton Jones of the University of Glasgow; it was named after the American mathematician Haskell Curry, who is known for his work in combinatory logic and the foundations of functional programming.

Haskell is a functional programming language known for its robust type system and types inference, as well as its support for higher-order functions and lazy evaluation. These features make it a powerful and expressive language well-suited to data processing, machine learning, and network programming tasks.

One of the critical characteristics of Haskell is its support for first-class functions, which can be passed as arguments, returned as results, or assigned to variables like any other data type. This allows for the creation of more flexible and reusable code. Haskell's lazy evaluation model also makes it easier to write efficient programs and to work with infinite data structures.

In addition to its technical features, Haskell has a large and active community of users and contributors who have developed a wide range of libraries, tools, and resources for tasks such as Web development, system administration, data processing, etc. Whether you are a new or experienced developer, Haskell is a language worth considering for your next project.

Let's look at how to install and set up its environment on our machine.

Setting up the development environment

Installation and Setup

Here are the steps to install and run Haskell on your machine:

  1. Download and install the GHCUp Haskell Platform:
  • Go to the Haskell Platform website (https://www.haskell.org/ghcup/)

  • Click on the "Download" button to redirect you to the recommended installation instructions.

  • Follow the prompts to install the Haskell Platform on your computer by copying and pasting the following URL into your terminal:

    This script downloads the ghcup binary into ~/.ghcup/bin/ And then runs an interactive installation that lets you choose various tool options. After installation, run the following command ghci --version to confirm the version; this also informs us if the Haskell toolchains have been downloaded successfully.

      curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
    

Haskell GHCi

GHCi (short for Glasgow Haskell Compiler Interactive mode) is an interactive interpreter for Haskell, which comes when GHC is installed. It is used to enter Haskell expressions, evaluate them, and load and run Haskell programs from files. In addition to evaluating expressions, GHCi provides other features helpful in developing Haskell programs.

  • Here are some of the commands:

  • - :load <module> : This command loads the specified Haskell module into GHCi

  • - :reload : This command reloads all the modules that have been loaded into GHCi

  • - :abandon : This command leaves the current execution and rests the program state.

  • - :t : This command displays the type of the expression.

  • - To find a complete command list, use the :help command

You can learn more about the GHC in the user guide. Next, let's explore some basic syntax of the language.

Basic Syntax

Let's look at how we can run simple expressions in GHCi. On your terminal, run the command. ghci .

ghci

The output should return the GHCi version and Prelude. Let's run some basic arithmetic operations and check our expressions type.

--Create a variable named `num`
[Prelude> let x = 3 * 4
-- Click Enter to save the variable in Prelude
-- Output the value of num by requesting the variable
[Prelude> x
-- Output
12


--Let's check the type of our output
[Prelude> :t x
x :: Num a => a

-- The data type of our output is a Number; it can also be called an Integer

We can do more than basic arithmetic operations in ghci terminal; we can interact with other data types, explicitly assign data types to expressions, define functions, etc.

Haskell also has a strong type of system that can help you catch errors and prevent runtime issues. Here is an example of a function that uses Haskell's type system to ensure that it is only called with arguments of the correct types:

add :: Int -> Int -> Int
add x y = x + y

This function takes two Ints, x and y, and returns their sum as and Int. The type signature Int -> Int -> Int specifies that add takes two Ints and returns an Int.

Haskell functions are first-class values treated just like any other data type. This means you can pass functions as arguments to other functions, return them as results, and assign them to variables just like you would with any additional value.

Here is a simple Haskell function that takes a list of integers and returns the sum of its elements:

sumList :: [Int] -> Int
sumList xs = foldl (+) 0 xs

This function uses the foldl function to iterate over the elements of the list xs and sum them up. The type signature [Int] -> Int specifies that sumList takes a list of Ints and returns an Int.

These are just a few basic syntaxes we will cover in this article.

Create a Simple Haskell Program

We have seen how great the ghci the compiler is at doing simple tasks, but it has a lot of limitations, such that we can't write and edit large programs and it doesn't support debugging. Therefore, an IDE and a build tool would help us navigate these problems. How can it be done in Haskell?

When we installed the Haskell toolchain earlier, an excellent tool for building Haskell projects was inclusive. Cabal is a system for building and packaging Haskell libraries and programs. We can create projects with the Cabal build tool. Let's run some commands to achieve this:

$ mkdir project-name
$ cd project-name
$ cabal init --interactive

This command initializes a few files:

Run the entire app by entering the following command in the root folder:

cabal run

Load a file using GHCi

We can also run a single Haskell source file; there are two ways we can achieve that. First, let's use a text editor to create a file named hello.hs. In the file, add the following source code:

main :: IO ()
main = do
    putStrLn "First Haskell Code"

To run the file, we can use either of these commands:

$ runghc hello.hs
$ ghci hello.hs

Also, we can load files in the ghci compiler by using the following command:

$ ghci 
$ [Prelude> :load hello.hs

NEXT STEPS

Congratulations on taking the first steps toward learning Haskell! This powerful and expressive functional programming language is well-suited for data processing, machine learning, and network programming thanks to its robust type system and support for high-level abstractions. These features make it easy to create correct and efficient programs that are easy to understand and maintain.

Furthermore, you can find Haskell being widely used in the blockchain industry. Haskell is the base language powering the Cardano ecosystem, as the Plutus and Marlowe DSLs used in writing Cardano-native smart contracts were built in Haskell.

We've covered you if you are looking for resources to help you get started. Check out Learn You a Haskell for Great Good!, a popular online tutorial that introduces Haskell concepts and syntax in a fun and engaging way. The HaskellWiki is also a comprehensive resource for all things Haskell, including tutorials, documentation, libraries, and more.

For more advanced concepts and techniques, consider picking up a copy of Real World Haskell, which shows you how to apply Haskell to real-world problems. Finally, the official Haskell website is an excellent place to find up-to-date information about the language and its community.

I would love to connect with you on LinkedIn | Twitter | Github | Portfolio