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=20260117T151347Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=52d3a137d85992af1169fdac37bf8a4d56a2d72296c3be4692a66fc136be5ac34433fcc784f66ec100398d5e5ed9e5650ef2317088d4e4c4cae1016bd36e866d65e283c3df746174b4dcd367d9944a6a3f29b8d81348a813e64bf6441ad27c04e4ba239922dbbb3e9cd01950c2f11019a24613d12f45a2f06f78e425744badb583077a5a1ef71998260a7fe43c3695fe503394444646c481b3b9926a380df7e6363c1c1c16a9f7aa842b65bf8e0421b09682ad240866c887ed365dd01033c1d7b35f3e6582baf83e97f11965e9a0e0832d370712c1debb86e4d5bebc56c1d09d984d47e5b72f49575ad9c82f89d49ac42352c5c7f8b6f6ef76193fbb97b6f6e5

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=20260117T151347Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=830d43882d5427b5a03986fde9affe89c7f2e326e0819112c1d8d782f77be7f5f21d348ab629a4e3b49b1bbfa7f0414b598f4880568cccd316550cc61e664d558784e9d7d6f13e4462d20952866b7be62499958d42f4b5961cf3469c311f0e6152b994f0547c8247c9e1bc2b8a059a23350659fd35ed6e735d83f97a15437ea4e264360113733c11eb1bdbbec04d0503d28c675858e6f8d1517a22ee8e17c1e091cadc7c5f438a492ce11dbd012edbecfef121f400c069185baddc17b85bfb8252f87094f963268835ee377a4fb60327fffd34fa7d5d925b3051bbc61e836bc9f9b4f39a3ce1615d6b9ed8fc671185d421b5022eaf140d5207dd7c9b6cf4e019

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=20260117T151347Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=034c7d3c42e28662b03dec588a143089c1e1118cb2dc3fe888ddbc0d84ff498a1ee4836a92132fe7b84eab696563106d7f78b80f289c7a5aecd9a4c6f706fc8249d542996ebb395f33687d975709cc045c03681801fb79c142799935ec2f7a705786c3a0e90b2aa2aef2af2260d8a2bc97b2630bb59b550aaee598b7d94dc05e20681a4e9a104af37b89ca5137f8b2adca5a0f55d663741ec36a394696d94f5254f9dc5456b238355cf23ddb09261aec907057937e9cc8ed409b3c365f0f800eed01b54310b11c106824e5f3ada98bfab4d3bf79c09d14d02bf51c9ff0740e04e2ede8bd6e99d59d315929844cbe5d7459ef70e8913968edf2d1476e608a3f9f

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=20260117T151347Z&X-Goog-Expires=3600&X-Goog-SignedHeaders=host&X-Goog-Signature=aa09ffbcbf38063721c3c9286bbc1a4e923548379e93a440038d159bb3c82564fbb4e6c1e202c0bdc86df7accde4217b0b5d095a51429d5e763da427e49f2d0d221164f9603998d0854220c4baee9a83aea5585296345bcdf22b34eaab0d66844d58dd330963607b10a6d65883146527b81cbd7d81210a9b5665eb2eb729275a96423a0307162243a6c8ac28e90becfe2020be7f25e14aecd23c6f8e64bc78c6d1ff465661d070e47bc735e4070566a5d73c97aeb41fd987863c5b18f8a410e9d68caf54b3f8a44ea5ba9cbdfdfc5352e30cbe84135767ecb1b6c637f75018a8136a1481f4563b6947dbda3b76a1cc8358f7e98bb1b28de4904d600d36b20311

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