Haskell for Rustaceans

by thelissimus

This is a free handbook for introducing Rustaceans to Haskell.

License

This work is licensed under Creative Commons Attribution 4.0 International License and code snippets are licensed under The 3-Clause BSD License.

Preface

I initially started working on this handbook as a help for my Rustacean friend who was trying to learn Haskell. After pondering for a while I figured that it would be useful to write a whole handbook altogether. To give back to community, hone my teaching and writing skills.

By no means I am an expert in neither of these languages. Therefore, I will only be writing about topics that I am fully confident on. I implore you to report mistakes if you find them. Since misinformation is the worst thing an author can do.

Introduction

Haskell and Rust are on the two different parts of the spectrum. Yet they are very close to each other.

Getting Started

In this chapter we will learn more about Haskell's tooling and ecosystem. Not much of a step-by-step tutorial that will hold you by your hands, but we will draw necessary parallels and put everything into its place.

Tooling

The Haskell tooling consists basically of the following:

ToolHaskellRust
Installerghcuprustup
Compilerghcrustc
Package managercabal-installcargo
Language Serverhaskell-language-serverrust-analyzer

Use GHCup to install the whole toolchain. You should figure it out pretty easily, it is quite similar to rustup and works on Windows, MacOS and Linux.

If you are using Linux, avoid using your distribution's package manager, unless you are using NixOS.

NOTE: GHCup doesn't work on NixOS. Use the Nix package manager to install the toolchain. Refer to the Haskell page on NixOS Wiki for further information.

More about package managers

If you have seen some projects or browsed Haskell related content on the internet there is a chance that you might have stumbled upon another Haskell package manger: stack. You might be wondering why did we chose cabal-install over stack. The answer is simple: cabal-install is arguably simpler. It doesn't try to manage GHC or any other tooling for you like stack does. There can be held a whole debate on to which one to choose. So we won't dwell on the topic. The main thing you need no know is: both of them use the same library under the hood anyways. cabal is a library that is used by both of the aforementioned package managers. We recommend using cabal.

Also, note that cabal-install provides the cabal executable. Hence, cabal-install is sometimes referred to as cabal for the sake of simplicity. We'll consistently use cabal-install to refer to the package and cabal for the library to avoid ambiguity. But be aware that in other sources or discussions, you might encounter cabal being used to mean either the library or the executable.

Ecosystem

Core Concepts

Data Types

Contents

Algebraic Data Types

Control Flow

Contents

If Expressions

Pattern Matching

Loops

Whoops! There are no loops! In Haskell one only has and uses recursion. You don't have to worry about overflowing the stack — it is on the heap.