/functional-programming

Primary LanguageHaskellApache License 2.0Apache-2.0

Functional Programming

Learning Haskell

If you want to learn Haskell from scratch, just like me, let's break down how to install it first:

1. Installation:

Installation is pretty simple. If you are using:

  • Windows:
Set-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { & ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -Interactive -DisableCurl } catch { Write-Error $_ }
  • Unix-like systems:

Make sure you have installed the following libraries I'm going to show bellow. These are dependencies to get the GHCup running as it is suposed to be. For more details, check here. In my case (Debian 12.7) I will install those dependencies

sudo apt update
sudo apt install curl build-essential libffi-dev libffi8 libgmp-dev libgmp10 libncurses-dev libncurses5 libtinfo5
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

Consider running those commands above in your terminal.

2. Getting Started

Note

The original step-by-step guide can be easily find at the ghcup website. What I am writing here is just a little briefing of this text. My main goal here is to make it simpler for you.

I'm creating files and studying in a Unix-like system, just in case you find here a cmd you are not used to.

Compiling Programs with GHC

The Glasgow Haskell Compiler (GHC) "can be used to compile Haskell modules and programs into native executables and libraries".

Note

GCH assumes you have a "main" module in your .hs program. If you don't use one, it will raise an exception.

Let's create our hello world example, hello.hs:

touch hello.hs && code hello.hs
main = putStrLn "Hello, World!"

Compile your code:

ghc hello.hs

Now, just run your program:

./hello
Hello, World!

Alternatively, you can interpret the file using the runghc

runghc hello.hs
Hello, World!

Warning

This is not a real warning. I'm just here to let you know that you can turn on warnings using the -Wall flag. This will help you improve your code in some other ways.

GHCI - The Interactive Environment

It's also called a Read-Evaluate-Print Loop (REPL). It's pretty simple to use and you can even create functions inside it.

ghci
GHCi, version 9.4.8: https://www.haskell.org/ghc/  :? for help
ghci> double n = n * 2
ghci> double 2
4

Also, you can load your haskell files into your environment and run it using the :load or :l command.

-- my fat.hs program
fat 0 = 1
fat n = n * fat(n - 1)
ghci> :l fat.hs
ghci> fat 3
6