Something went wrong. Try again.
Jam Programming Language jamlang.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149---layout: tourtitle: A Tour of Jam - Basicspermalink: /tour/progress: 25current_chapter: 1total_chapters: 4chapter_title: The Basicschapter_intro: Welcome to the tour. This guide will walk you through the fundamental building blocks of Jam. By the end, you'll be ready to write your first sweet application.chapters: - number: "01" name: Basics active: true - number: "02" name: Control Flow active: false - number: "03" name: Types active: falsenext_chapter: url: "#" title: Control Flow & Logic description: Learn about if, else, and pattern matching.---
<!-- Section 1: Variables --><div class="grid lg:grid-cols-2 gap-12 lg:gap-20 items-start group"> <!-- Left: Explanation --> <div class="lg:sticky lg:top-32 order-2 lg:order-1"> <span class="text-4xl font-bold step-number opacity-50 mb-4 block">01</span> <h2 class="text-2xl font-bold text-slate-900 mb-4">Variables & Binding</h2> <div class="prose prose-slate prose-lg text-slate-600"> <p class="mb-4"> In Jam, we use <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">const</code> for immutable bindings and <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">var</code> for mutable ones. </p> <p class="mb-4"> Using <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">const</code> by default helps prevent bugs where data changes unexpectedly. </p> <p> If you need to change a value, use <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">var</code> to make it mutable. </p> </div> </div>
<!-- Right: Code --> <div class="order-1 lg:order-2"> <div class="code-block rounded-xl shadow-2xl overflow-hidden transform transition-transform duration-500 hover:scale-[1.01]"> <div class="code-block-header flex items-center justify-between px-4 py-3"> <span class="text-xs text-slate-400 font-mono">variables.jam</span> <div class="flex space-x-1.5"> <div class="w-2.5 h-2.5 rounded-full bg-slate-600"></div> <div class="w-2.5 h-2.5 rounded-full bg-slate-600"></div> </div> </div> <div class="p-6 overflow-x-auto"> <pre class="text-sm font-mono leading-relaxed"><span class="syn-keyword">const</span> std = <span class="syn-function">import</span>(<span class="syn-string">"std"</span>);
<span class="syn-comment">// Immutable binding</span><span class="syn-keyword">const</span> name: <span class="syn-type">str</span> = <span class="syn-string">"Strawberry"</span>;<span class="syn-comment">// name = "Grape"; // Error! Cannot reassign const</span>
<span class="syn-comment">// Mutable binding</span><span class="syn-keyword">var</span> count: <span class="syn-type">u8</span> = <span class="syn-number">10</span>;count = <span class="syn-number">11</span>; <span class="syn-comment">// This is fine with var</span>
std.fmt.<span class="syn-function">println</span>(<span class="syn-string">"Making jam!"</span>);</pre> </div> </div> </div></div>
<!-- Section 2: Functions --><div class="grid lg:grid-cols-2 gap-12 lg:gap-20 items-start group"> <!-- Left: Explanation --> <div class="lg:sticky lg:top-32 order-2 lg:order-1"> <span class="text-4xl font-bold step-number opacity-50 mb-4 block">02</span> <h2 class="text-2xl font-bold text-slate-900 mb-4">Making Functions</h2> <div class="prose prose-slate prose-lg text-slate-600"> <p class="mb-4"> Functions are declared with <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">fn</code>. They are strictly typed, ensuring you always know what goes in and what comes out. </p> <p class="mb-4"> Return types are specified directly after the parameter list. Use <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">return</code> to return values from a function. </p> </div> </div>
<!-- Right: Code --> <div class="order-1 lg:order-2"> <div class="code-block rounded-xl shadow-2xl overflow-hidden transform transition-transform duration-500 hover:scale-[1.01]"> <div class="code-block-header flex items-center justify-between px-4 py-3"> <span class="text-xs text-slate-400 font-mono">functions.jam</span> </div> <div class="p-6 overflow-x-auto"> <pre class="text-sm font-mono leading-relaxed"><span class="syn-keyword">fn</span> <span class="syn-function">addSugar</span>(amount: <span class="syn-type">i32</span>) <span class="syn-type">i32</span> { <span class="syn-keyword">return</span> amount + <span class="syn-number">10</span>;}
<span class="syn-keyword">fn</span> <span class="syn-function">greet</span>() { <span class="syn-comment">// No return type = void function</span> std.fmt.<span class="syn-function">println</span>(<span class="syn-string">"Hello!"</span>);}
<span class="syn-keyword">fn</span> <span class="syn-function">main</span>() <span class="syn-type">u8</span> { <span class="syn-function">greet</span>(); <span class="syn-keyword">return</span> <span class="syn-number">0</span>;}</pre> </div> </div> </div></div>
<!-- Section 3: Imports & Standard Library --><div class="grid lg:grid-cols-2 gap-12 lg:gap-20 items-start group"> <!-- Left: Explanation --> <div class="lg:sticky lg:top-32 order-2 lg:order-1"> <span class="text-4xl font-bold step-number opacity-50 mb-4 block">03</span> <h2 class="text-2xl font-bold text-slate-900 mb-4">Imports & Standard Library</h2> <div class="prose prose-slate prose-lg text-slate-600"> <p class="mb-4"> Jam uses <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">import()</code> to bring in modules. The standard library provides common utilities. </p> <p> Use <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">std.fmt</code> for formatting and printing, <code class="text-sm bg-pink-50 text-pink-700 px-1 py-0.5 rounded font-mono">std.thread</code> for timing and threading. </p> </div> </div>
<!-- Right: Code --> <div class="order-1 lg:order-2"> <div class="code-block rounded-xl shadow-2xl overflow-hidden transform transition-transform duration-500 hover:scale-[1.01]"> <div class="code-block-header flex items-center justify-between px-4 py-3"> <span class="text-xs text-slate-400 font-mono">imports.jam</span> </div> <div class="p-6 overflow-x-auto"> <pre class="text-sm font-mono leading-relaxed"><span class="syn-keyword">const</span> std = <span class="syn-function">import</span>(<span class="syn-string">"std"</span>);
<span class="syn-keyword">fn</span> <span class="syn-function">main</span>() <span class="syn-type">u8</span> { std.fmt.<span class="syn-function">println</span>(<span class="syn-string">"Hello, World!"</span>); std.fmt.<span class="syn-function">print</span>(<span class="syn-string">"Waiting..."</span>); std.thread.<span class="syn-function">sleep</span>(<span class="syn-number">1000</span>); <span class="syn-comment">// 1 second</span> std.fmt.<span class="syn-function">println</span>(<span class="syn-string">"Done!"</span>); <span class="syn-keyword">return</span> <span class="syn-number">0</span>;}</pre> </div> </div> </div></div>