Haskell Tutorial for Beginners Pure Functional Programming

Zaheer Ahmad 4 min read min read
Python
Haskell Tutorial for Beginners Pure Functional Programming

Introduction

Haskell is a powerful programming language that focuses on pure functional programming, making it very different from languages like Python, Java, or C++. This Haskell tutorial for beginners: pure functional programming is designed to help Pakistani students understand the fundamentals of Haskell in a simple, structured way.

If you are looking to learn Haskell from scratch, this guide will walk you through the core ideas, syntax, and real-world usage. Haskell emphasizes immutability, strong typing, and mathematical functions, which makes it highly reliable and suitable for complex systems like financial software, compilers, and research tools.

For Pakistani students in cities like Lahore, Karachi, or Islamabad, learning functional programming Haskell can open doors to international opportunities, research roles, and advanced software engineering careers. It also strengthens problem-solving skills and logical thinking.

Prerequisites

Before starting this Haskell tutorial, you should have:

  • Basic understanding of programming concepts (variables, functions)
  • Familiarity with any language like Python, C++, or Java
  • Basic mathematical thinking (functions, logic)
  • A computer with Haskell installed (GHC and GHCi recommended)

Don’t worry if you’re new—this tutorial is beginner-friendly.


Core Concepts & Explanation

Pure Functions and Immutability

In Haskell, functions are pure, meaning:

  • Same input always produces the same output
  • No side effects (no changing global variables, no hidden state)

Example:

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

Explanation:

  • add is a function
  • :: defines the type (takes two Ints, returns an Int)
  • x + y is the output
  • No variables are changed—everything is immutable

This makes programs predictable and easier to debug.


Strong Static Typing

Haskell has a powerful type system that catches errors early.

Example:

square :: Int -> Int
square x = x * x

Explanation:

  • square takes an integer and returns an integer
  • If you pass a string, Haskell will give an error before running

This is very useful for students building reliable systems like banking apps handling PKR transactions.


Algebraic Data Types (ADT)

Haskell allows you to create custom data types.

data Shape = Circle Float | Rectangle Float Float

Explanation:

  • Shape is a new type
  • It can be either:
    • Circle with one Float (radius)
    • Rectangle with two Floats (length, width)

This is useful for modeling real-world data like student records or bank accounts.


Type Classes

Type classes define behavior for types.

class Eq a where
  (==) :: a -> a -> Bool

Explanation:

  • Eq is a type class for equality
  • (==) checks if two values are equal

Example usage:

5 == 5  -- True

Monads (Simplified)

Monads help manage computations like input/output or errors.

Example with Maybe:

safeDivide :: Float -> Float -> Maybe Float
safeDivide _ 0 = Nothing
safeDivide x y = Just (x / y)

Explanation:

  • If dividing by zero → Nothing
  • Otherwise → Just result

Practical Code Examples

Example 1: Basic Calculator

main :: IO ()
main = do
  putStrLn "Enter first number:"
  x <- readLn
  putStrLn "Enter second number:"
  y <- readLn
  let sum = x + y
  putStrLn ("Sum is: " ++ show sum)

Line-by-line Explanation:

  • main :: IO () → Entry point of program
  • do → Used for sequencing actions
  • putStrLn → Prints text to screen
  • x <- readLn → Takes input from user
  • let sum = x + y → Calculates sum
  • show sum → Converts number to string
  • ++ → Concatenates strings

This can be used by a student like Ahmad to build a simple CLI calculator.


Example 2: Real-World Application (Student Grades)

grade :: Int -> String
grade marks
  | marks >= 80 = "A"
  | marks >= 60 = "B"
  | marks >= 40 = "C"
  | otherwise = "Fail"

main :: IO ()
main = do
  putStrLn "Enter marks:"
  m <- readLn
  putStrLn ("Grade: " ++ grade m)

Explanation:

  • grade :: Int -> String → Function returns grade
  • | → Guards (like if-else)
  • otherwise → Default condition
  • m <- readLn → Input marks
  • Output shows grade

Useful for schools in Pakistan to calculate results automatically.


Common Mistakes & How to Avoid Them

Mistake 1: Thinking Imperatively

Many beginners try to write Haskell like C++.

❌ Wrong:

x = 5
x = x + 1

✅ Correct:

let x = 5
let y = x + 1

Fix:

  • Variables are immutable
  • Create new values instead of modifying

Mistake 2: Ignoring Types

Beginners often skip type declarations.

❌ Risky:

add x y = x + y

✅ Better:

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

Fix:

  • Always define types
  • Helps catch errors early

Practice Exercises

Exercise 1: Even or Odd

Problem:
Write a function to check if a number is even.

Solution:

isEven :: Int -> Bool
isEven x = x `mod` 2 == 0

Explanation:

  • mod gives remainder
  • If remainder is 0 → even number

Exercise 2: Calculate Discount

Problem:
Ali runs a shop in Karachi. If purchase > 5000 PKR, give 10% discount.

Solution:

discount :: Float -> Float
discount amount
  | amount > 5000 = amount * 0.9
  | otherwise = amount

Explanation:

  • amount * 0.9 → 10% discount
  • Otherwise → no discount

Frequently Asked Questions

What is Haskell used for?

Haskell is used for building reliable software, compilers, financial systems, and academic research. Its strong type system makes it ideal for error-free applications.

How do I install Haskell?

You can install Haskell using the Haskell Platform or GHC (Glasgow Haskell Compiler). After installation, use ghci to start coding interactively.

Is Haskell hard for beginners?

Haskell can feel different at first, especially if you're used to imperative programming. However, with practice and the right guidance, it becomes easier and very rewarding.

How do I practice Haskell effectively?

Practice by solving small problems daily, using GHCi, and building mini projects like calculators or student grading systems.

Can I get a job with Haskell in Pakistan?

While Haskell jobs are limited locally, it is highly valued internationally. Remote work opportunities are available, especially in fintech and research domains.


Summary & Key Takeaways

  • Haskell is a pure functional programming language
  • It emphasizes immutability and strong typing
  • Algebraic Data Types help model real-world problems
  • Monads manage complex operations like I/O and errors
  • Writing type-safe code reduces bugs significantly
  • Ideal for advanced programming and international careers

To continue your journey, explore these related tutorials on theiqra.edu.pk:

  • Learn more about functional programming concepts to deepen your understanding
  • Start a Python tutorial for beginners to compare paradigms
  • Explore data structures and algorithms for problem-solving skills
  • Try a Rust programming tutorial for systems-level programming

By combining Haskell with other technologies, Pakistani students can build a strong and versatile programming skill set.

Practice the code examples from this tutorial
Open Compiler
Share this tutorial:

Test Your Python Knowledge!

Finished reading? Take a quick quiz to see how much you've learned from this tutorial.

Start Python Quiz

About Zaheer Ahmad