Pascal
The classic language for learning to program.
Pascal was designed by Niklaus Wirth to teach structured programming clearly. For decades it was the first language in schools and universities, and through Delphi and Free Pascal it still builds fast desktop applications today.
Why learn Pascal?
Pascal's clear structure makes it one of the best languages for understanding how programs are built step by step.
- Readable:
begin ... endblocks and clear keywords. - Strict types: catches mistakes before the program runs.
- Fast native apps: Lazarus and Delphi build GUI apps quickly.
Set up and run your first program
Install Free Pascal (fpc). Lazarus adds a visual IDE for desktop apps.
sudo apt install fpc
fpc hello.pas
./helloprogram Hello;
var
name: string = 'Ada';
begin
writeln('Hello, AI! Welcome to Pascal, ', name, '.');
end.end. (a full stop).Variables and types
Variables are declared in a var section before begin. Assignment uses :=.
program Variables;
var
layers: integer;
params, gb: real;
model: string;
begin
layers := 32;
params := 8.03e9;
model := 'llama3.1';
gb := params * 2 / 1e9;
writeln(model, ': ', layers, ' layers, ', gb:0:1, ' GB');
end.Control flow
Pascal has if, case, for, while and repeat ... until. Use begin ... end to group several statements.
program Control;
var epoch, bits: integer;
begin
for epoch := 1 to 3 do
writeln('epoch ', epoch);
bits := 8;
case bits of
32: writeln('FP32');
16: writeln('FP16');
1..8: writeln('low precision');
else
writeln('other');
end;
end.Procedures and functions
Procedures do work; functions return a value through Result. var parameters let a procedure change its argument.
program Procs;
{$mode objfpc}
function Params(layers, width: Int64): Int64;
begin
Result := 12 * layers * width * width;
end;
procedure Double(var x: integer);
begin
x := x * 2;
end;
var n: integer = 21;
begin
writeln(Params(32, 4096));
Double(n);
writeln(n);
end.Arrays and records
Arrays have declared index ranges. Records group related fields into one type.
program Records;
type
TGpu = record
name: string;
vram: integer;
end;
var
gpus: array[1..3] of TGpu;
i, total: integer;
begin
gpus[1].name := 'H100'; gpus[1].vram := 80;
gpus[2].name := 'H200'; gpus[2].vram := 141;
gpus[3].name := 'RTX 5090'; gpus[3].vram := 32;
total := 0;
for i := 1 to 3 do
begin
if gpus[i].vram >= 80 then writeln(gpus[i].name, ' is a data center GPU');
total := total + gpus[i].vram;
end;
writeln('Total VRAM: ', total, ' GB');
end.Classes (Object Pascal)
Free Pascal and Delphi add classes with constructors, methods and properties.
program Classes;
{$mode objfpc}{$H+}
type
TGpu = class
Name: string;
Vram: integer;
constructor Create(AName: string; AVram: integer);
function Fits(gb: integer): boolean;
end;
constructor TGpu.Create(AName: string; AVram: integer);
begin Name := AName; Vram := AVram; end;
function TGpu.Fits(gb: integer): boolean;
begin Result := gb <= Vram; end;
var g: TGpu;
begin
g := TGpu.Create('RTX 5090', 32);
writeln(g.Name, ' fits 16 GB: ', g.Fits(16));
g.Free;
end.AI with Pascal
Pascal is not a common AI language, but it is a great way to learn the maths behind AI without hidden magic. Free Pascal has neural network libraries such as CAI Neural API, and Delphi can call Python AI libraries through Python4Delphi or LLM APIs over HTTP.
The essential libraries
Gradient descent from scratch
Fit y = 3x + 0.5 with plain loops.
program Gradient;
const N = 100;
var
xs, ys: array[1..N] of real;
w, b, gw, gb, err: real;
i, step: integer;
begin
for i := 1 to N do
begin
xs[i] := -1 + 2 * (i - 1) / (N - 1);
ys[i] := 3 * xs[i] + 0.5;
end;
w := 0; b := 0;
for step := 1 to 500 do
begin
gw := 0; gb := 0;
for i := 1 to N do
begin
err := w * xs[i] + b - ys[i];
gw := gw + 2 * err * xs[i] / N;
gb := gb + 2 * err / N;
end;
w := w - 0.1 * gw;
b := b - 0.1 * gb;
end;
writeln('w = ', w:0:2, ' b = ', b:0:2);
end.Call an LLM from Pascal
Free Pascal's fphttpclient and fpjson units can send a chat request to a local model and read the reply. A tool-calling loop follows the same steps as in the other volumes.
program Chat;
{$mode objfpc}{$H+}
uses SysUtils, Classes, fphttpclient, fpjson, jsonparser;
var
client: TFPHTTPClient;
body, reply: string;
data: TJSONData;
begin
body := '{"model":"llama3.1","messages":[{"role":"user","content":"Explain tensor cores in one sentence."}]}';
client := TFPHTTPClient.Create(nil);
try
client.AddHeader('Content-Type', 'application/json');
client.RequestBody := TStringStream.Create(body);
reply := client.Post('http://localhost:11434/v1/chat/completions');
data := GetJSON(reply);
writeln(data.FindPath('choices[0].message.content').AsString);
data.Free;
finally
client.RequestBody.Free;
client.Free;
end;
end.To add tools, include a tools array in the body and check choices[0].message.tool_calls in the reply.
ollama pull llama3.1, and keep it running. The code talks to its OpenAI-compatible API at http://localhost:11434/v1, so the same program also works with vLLM, llama.cpp server or a cloud API by changing the URL, model name and adding an API key.Pascal learning roadmap
Tick items off as you go. Your progress is saved in this browser.
Beginner
Intermediate
Advanced
Projects to build
Pascal cheat sheet
| Task | Syntax |
|---|---|
| Variable | var x: integer; |
| Assign | x := 5; |
| Loop | for i := 1 to 10 do |
| Block | begin ... end; |
| Function | function F(x: integer): integer; |
writeln('Hi ', x); |
Common mistakes
- Use
:=to assign and=to compare. - No semicolon before
elsein an if statement. - Strings are 1-indexed: the first character is
s[1].
Where to go next
- Free Pascal — docs
- Lazarus — IDE
- Learn Pascal (wiki) — tutorial
On TensorCore.si: C volume, Ada volume.
Keyboard: [ previous language · ] next language · / search the library