getbetterat.work

Write code that
actually works.

OCaml is a tool for building computer programs. It is famous for two things: running very fast and catching your mistakes before you even run the code.

The "Functional" Idea

Most coding languages change data as they go. This is like trying to read a book while someone else is erasing words. It causes bugs.

OCaml is a functional language. This means it treats functions like math machines. You put data in. You get brand new data out. The old data stays perfectly safe and untouched.

OLD DATA (Number: 5)
MATH MACHINE (Add 10)
!
CHANGED OLD DATA
NEW DATA (15)
user_name = "Sam"
Read Only

The "No Eraser" Rule

In OCaml, variables are locked by default. Once you say a box contains the number 5, it contains the number 5 forever. You cannot change it later.

This sounds crazy at first. But it solves a massive problem. If code cannot change data secretly in the background, your programs become completely predictable. If you want a new number, you simply make a new box.

The Smart Proofreader

User typed:

add_numbers("Apple", 5)
ERROR: "Apple" is a word, not a number.

In old languages, you have to tell the computer exactly what kind of data you are using. "This is a word. This is a number."

OCaml uses Type Inference. This is a big word that means "guessing correctly". The OCaml computer reads your code and instantly figures out what the data is. If you make a silly mistake, it stops you right away. It catches bugs before humans can even see them.

The Exact Red Pen

When bad code breaks, normal tools just yell, "Something broke at line 500." They leave you to hunt for the problem yourself.

OCaml does not make you hunt. It takes a red pen and underlines the exact word that caused the problem. It tells you what it expected, and what you gave it instead. It acts like a helpful coach, not an angry alarm.

let my_math = 10 + "Hello"
This expression has type string
but an expression was expected of type int

The Smart Sorter

Instead of writing long lists of "if this happens, then do that", OCaml uses Pattern Matching. Think of it like a mailroom sorting machine.

Input Shape
Match: Empty Box Do Action A
Match: Box with 2 items Do Action B
Match: Anything Else Do Action C

The best part? If you forget to add a mail slot for a specific type of package, the OCaml tool will warn you before you run the code. It makes missing cases impossible.

Loop 1
Loop 2
Done! Return Answer.

The Repeating Trick

Many languages use commands like "for" and "while" to do things over and over. This requires keeping track of counting numbers, which can lead to mistakes.

OCaml loves Recursion. Instead of a spinning wheel, a function simply calls itself again, passing along the new data, until the job is completely finished. It is cleaner, safer, and reads more like plain math.

Code That Explains Itself

Other Languages

You have to write huge manual notes so humans know what the code does.

# WARNING: THIS FUNCTION REQUIRES:
# 1. A string for the name
# 2. An integer for the age
# 3. IT WILL RETURN a True or False
# DO NOT PASS DECIMALS!!!
def check_user(name, age):
  ...

OCaml

The code's "Type Signature" IS the manual. It cannot lie. It is checked by the computer.

val check_user :
string -> int -> bool

(* That's it. Code reading software instantly knows what goes in and out. *)

Two Ways to Build

OCaml does not force you to follow just one set of rules. It gives you choices.

STYLE 1

Functional Mode

Follow the strict rules of math. Never change data directly. Make copies. This is the safest way to write code. It is perfect for complex math or safe banking software.

STYLE 2

Imperative Mode

Sometimes you just need to get things done quickly. In this mode, you give the computer direct orders to change data right now. OCaml lets you break the strict rules when you really need to.

The Automatic Janitor

When a computer program runs, it uses memory to store data. If you don't clean up old data, the computer runs out of space and crashes.

OCaml has a built-in "Garbage Collector". Think of it as a super-fast, invisible janitor. It constantly scans your program, finds data you are no longer using, and throws it away for you. You never have to write cleanup code yourself.

Old Name
Old Math
DELETED
Scanning for unused memory...

Building With Lego

When your project gets huge, OCaml lets you organize it beautifully using "Modules".

Module: Users

Handles logins and passwords. Locked in its own box.

Module: Store

Connects to the 'Users' box perfectly without breaking it.

Module: Math

Stand-alone calculator. Swap it out anytime.

Modules act like physical Lego pieces. They have clear bumps on top and holes on the bottom. You always know exactly how two pieces of code will click together.

Python (Safe but Slow)
C / C++ (Fast but Unsafe)
OCaml (Fast AND Safe)

Raw Machine Speed

Many safe languages are very slow. They have a hidden helper running in the background to keep things safe.

OCaml does not need a background helper. Its proofreader does all the work *before* the code runs. Once the code is checked, OCaml turns it into raw machine code. This makes it run incredibly fast, almost as fast as low-level languages like C.

The Testing Playground

Waiting for code to load is boring. OCaml comes with a tool called the REPL (Read-Eval-Print Loop).

It acts like a scratchpad. You type a single line of code, press enter, and the computer gives you the answer instantly. It is the best way to test small ideas before putting them into your main project.

# 10 * 5 ;;
- : int = 50
# String.uppercase_ascii "hello" ;;
- : string = "HELLO"
#

Big Teams Trust It

A massive trading firm uses it to move billions of dollars safely every day.

One of the world's biggest social networks uses a version of it to check code.

Famous software packing tools were originally built completely in OCaml.

Security teams use it to prove that security locks are impossible to break.

Where is OCaml Used?

Financial Systems

Banks and trading companies use it. If a bank makes a math error, they lose millions of dollars. They use OCaml because it catches errors before they happen.

Compilers

A compiler is a tool that reads code and turns it into software. Because OCaml is so good at reading rules and logic, it is the best tool for building other tools.

Formal Verification

This means "proving with math that something is 100% correct". People use OCaml to prove that airplane software or microchips have absolutely no bugs.

The OCaml Toolkit

opam
The package manager. It downloads tools built by other people.
dune
The builder. It puts all your files together into one final program.
ocamlopt
The fast compiler. It turns your code into super-fast machine code.

See the Code

Look at how short and clean the code is. We are making a small tool called add. It takes two items, x and y, and adds them together.

Notice that we never said "x is a number". The OCaml smart checker sees the + sign and instantly knows they must be numbers. Less typing for you, but still 100% safe.

let add x y =
  x + y

(* The system now knows: *)
(* add : int -> int -> int *)

Why Pick OCaml?

Feature Python Java OCaml
Catches errors early? No (Crashes later) Yes (But lots of typing) Yes (With zero typing)
Running Speed Slow Medium Very Fast
Style Flexibility Mostly Imperative Mostly Object-Oriented Both (Functional + Imperative)

Your First 10 Minutes

1

Install Opam

Download the main tool manager. It will set up your computer automatically.

Open the Playground

Type utop in your terminal. You can instantly start writing code lines to see how they work.

2
3

Build a File

Write a small file. Use the Dune builder tool to turn it into a super-fast program.