Arun Pandian M

Arun Pandian M

Android Dev | Full-Stack & AI Learner

Types & Functions — The Real Building Blocks of Programs

Types tell you what something is. Functions tell you what it can do.

Before we talk about category theory and FP, we must get two things perfectly clear:

• What are types?

• What are functions?

Let’s walk through them like a story.

The Typing Monkeys Story

https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/monkey_type.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260117%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260117T134257Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=6693be29cf25207b040f7e5e0e3f527c8476f4580e199ba4db5add33ae560eb1647fea2c4513d1410604f9fff189f2bde920eab2bca1dc09a33dbf47a8f2f4dd58e8e43f92163b8eb493774e8061ddf4b7b66fcd600b800968b787e833b0df8446ac3fd98fc9e56c7762424ddaa98aa5f50f1ecb488f4c6e299841636fc8eb78a145073c3fb02d78018ed39f643b45ee09ee1205d73a03f952e35981696592be1d6277d130654bd993cadad897d3738b6fc7b03ca65fdc69afd8aaaf23519d691bb1e26bbd509210e006f5b5a7b5388e5738f755481de2f1b4739cfa23f481a81263dd304e0cd74ae73108740b4eec0a80d1f2e0bdd89a916d7112b02588545d

Imagine millions of monkeys sitting in front of computers, banging on keyboards, trying to accidentally write a valid program.

• In machine code, any random bytes are “valid,” so monkeys are happy — everything runs.

• In dynamically typed languages, the program may run… until it suddenly crashes because someone added a number to a banana.

• In statically and strongly typed languages, most monkey-written programs don’t even compile

That sounds cruel… but here’s the twist:

Fewer happy monkeys → More correct programs

Typing is like having:

• A grammar checker

• A spell-checker

• A logical consistency checker (type checker)

All working together before the program even runs.

If Romeo is declared a human, he shouldn’t grow wings or become a black hole halfway through the story

Types stop nonsense early.

Why Do We Need Mathematical Models for Programming?

Because programs are compositions of pieces, just like:

• Lego blocks

• Electrical circuits

• Water pipes

But compositions only work if the ends match.

In category theory:

You can only compose arrow f: A → B with arrow g: B → C because B matches B.

In programming:

You can only pass output of function f into function g if f returns the type g expects.

Types ensure the ends match.

Without types, you’re mixing pipes of different diameters and hoping nothing explodes.

What Are Types (Really)?

https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/what_are_types.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260117%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260117T134257Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=86dcb6e935b296f5ab763d425871bba87b5e72a48f4b169a48bdd00e275fbf64f163500b87830e186e99c148ceae5988012a30207127a5dbbb432f2830bc7e838b4ac58b06ee82bc713e8b7163198bfd17ff4d43c24774f2de26b5aa8b7ae34cccd088b017bd6e47771b647bf6f97ae016f70935880079107e6f05ffa38ddf5fc9752bb5314dbc0e0d67d045f56e86faca0924f92888b0c8d6aff686b624d42aac599c5496e8321f1e647081ce4c975a56923a1ed063b3e41c07962268e9c10687ba7a04b925644405128fd0485f381594e9aabb37f4fe50b1df7ac93d095ea584ed13cf8e354bdb90a602c3bd89e08e122599a28fe95666d4f28609d8ec3e52

Here’s the clean mathematical idea:

A Type = A collection of possible values + the rules that apply to them.

For example:

Int   → {… −2, −1, 0, 1, 2 …}
String → {"hello", "world", "…"}
Boolean → {true, false}

A type tells you:

• What operations are allowed

• What values are valid

• What functions can accept it

• How two parts of a program can fit like puzzle pieces

Weak vs. Strong Typing

Weak typing lets you mix things freely.("5" + 2 == "52" in JavaScript)

Strong typing forces you to be explicit. No accidental merging of bananas and booleans.

Simple Analogy

Weak typing → A kitchen where all bottles look the same

Strong typing → Bottles are clearly labeled: “salt,” “vinegar,” “oil”

Which kitchen produces better food?

Static vs. Dynamic Typing

Dynamic typing: errors appear when running the program

Static typing: errors appear before running

Analogy:

Dynamic typing = discovering the airplane has a missing bolt while flying

Static typing = discovering it in the hangar

Which one is safer?

What Are Functions?

https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/function_what_are.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260117%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260117T134257Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=922970009fa4fed3f786f623ee95941cf10dca32e5b4bd0007276bdabcf3598fb475c05708a92f054b7b14246a551fda0b2de472aa08be8f1d2d637053cec8f878c1081302c679a5e36ca326f0bcaaab1b3c936ac03994885ec4bf1829a61de741fa0c8b8809a2471941a6e23c931c2ef54b9bc38ec29236e6b5af4361a4cbc9301a93a82bb9a12e0ac3d3191fd19dc3fb25282a24f20e9f2b0448c998f8ac8f6ee638659bd0873f81cc177664ee464fcf995073f1b5a9d0c145be8c64744e806b53de36226ae968b1cd9e3917aab4f4a27a32883993dafc4c15344b5c55ba8046fc72a6cb1e2345f602af942b8678072b8aa644c39e2c5f95470078275b2ccc

This is the heart of FP.

A Function = A machine that takes input and gives output

Mathematically:

A function is a mapping from values of one type to values of another type.

f: A → B

In Kotlin:

val length: (String) -> Int = { it.length }

Functions:

• Take something of type A

• Produce something of type B

• Must not mutate the input

• Always return the same output for the same input (purity)

Functions as Arrows (Category Theory POV)

In category theory:

Objects → Types

Arrows → Functions

Composition → Piping outputs into inputs

A --f--> B --g--> C

Becomes:

val f: (A) -> B
val g: (B) -> C
val h = g compose f

Why Functions Matter: They Make Composition Possible

Composition only works if:

• The output type of f matches the input type of g

• Types ensure correctness

• Functions ensure transformation

• Together they form composable systems

This is why FP loves functions and types:

They allow you to break big chaos into small perfect transformations.
https://storage.googleapis.com/lambdabricks-cd393.firebasestorage.app/composition_analogy.svg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=firebase-adminsdk-fbsvc%40lambdabricks-cd393.iam.gserviceaccount.com%2F20260117%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20260117T134257Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=5fd3e03bd450ffd6c63951fe823777b9a3e21f2172cf548c549ee650411a737916fc8bf0e9c4a37ddc425c1c7033453e0e2228d210a79467f83170c59bbac37ec04ddaccb0b59be297a6b2d6eae579096bcb6e69f65aa101176b31558744ab5ab0aaf13361d08ec8af4e23922f91d6e68566ed72743242d7e37fa74e78d33d609b3864fc04a338146ac6bbe61373b1b42193ef043645792fccf8a9d98f4bef1b388277e31ba20ef5bb0c34014206bb1f69a21c2ceb1b003826b70b05ec40b03de8ec9cd7275f661a2063ac9998be115410040281ba52a3f404b0e135465d7fd1602a4733946d93207f115b1b93ba4edcd2278b2027b0024dd5f72b647ab31837

Think of it like running a small café.

Each person in your kitchen has one job:

• One chef cuts vegetables

• Another cooks them

• Another plates the dish

Each station expects a specific kind of input.

The chopping chef can only handle raw vegetables, not fully cooked food.

The cooking chef expects chopped vegetables, not whole carrots.

The plating chef expects cooked food, not raw ingredients.

If any step receives the wrong kind of input, the whole workflow falls apart.

Functions work exactly the same way.

• A function takes a specific “ingredient” (type).

• It transforms it into a new “dish” (another type).

• Only when the dish matches the next chef’s expectation can it move forward.

That’s why FP cares so much about types and functions:

They turn a messy kitchen into a perfect assembly line, where every step is safe, predictable, and composable — and where you can swap out or improve one step without breaking everything else.

Functions and types are the backbone of composition. Types make sure every step receives the right kind of input, and functions transform that input into the next meaningful output. This simple discipline — the right thing in, the right thing out — turns complex systems into predictable, composable flows. Just like an ATM works by chaining small steps, functional programs work by connecting small, reliable transformations.

In the next post, we’ll go one level deeper and explore Function Signatures and Pure Functions. You’ll learn how signatures describe the behavior of functions even before writing code, why purity matters for reliability, and how real-world systems benefit from deterministic functions. This sets the stage for understanding higher-order functions and more advanced composition patterns.

#kotlin_fp#category_theory#functional_programming#pure_functions#function_composition#type_theory#types_in_programming#function_basics#strong_typing#static_typing#type_safety#programming_foundations#developer_mindset#mathematical_programming#clean_code_principles