Fundamentals

Implemented — the fundamentals examples are real, checked-in programs under examples/01-fundamentals/ (15 targets) that round-trip the language surface on both engines (the Go front end and the self-hosted kcc engine), producing byte-identical stdout and exit codes. Each file is gated by pkg/cli/phase114_examples_test.go.

A deeper multi-file round-trip corpus (variables → functions → recursion → arrays → strings → control flow → structs → maps → types → match → methods → modules → application layout) also lives under examples/language_foundation/ (13 targets, Phase 102).

examples/01-fundamentals/

File

Demonstrates

01_hello_world.kark

The obligatory first program

02_variables.kark

let / var bindings and reassignment

03_constants.kark

const immutable local bindings (Phase 112)

04_functions.kark

Functions, returns and composition

05_conditionals.kark

if / else if / else chains (grade classification)

06_loops.kark

while loops, push, reversal, Fibonacci accumulation

07_strings.kark

Concatenation, len, indexing, slices, casts

08_arrays.kark

Index access, mutation, slices, 2-D arrays, iteration

09_maps.kark

Map construction/access with string and integer keys, plus std.collections helpers

10_structs.kark

Record types, literals, field reads and mutation

11_match.kark

match on Option and literal-bound values

12_casts.kark

int(f) / str(n) / float-mixing casts

13_enums.kark

enum variants, EnumName.Variant tags and matching

14_adt_match.kark

ADT-style tagged match over enum values

15_closures.kark

let f = fn(...) closures with captured variables

Hello world

01_hello_world.kark:

func main() {
    println("Hello, Karkain!")
}

Run it:

$ karkain run examples/01-fundamentals/01_hello_world.kark

Expected output:

Hello, Karkain!

A walk through the beginner path:

  1. 01_hello_world.kark — print to stdout

  2. 02_variables.kark + 03_constants.kark — bindings and constants

  3. 04_functions.kark — program structure

  4. 05_conditionals.kark + 06_loops.kark — control flow

  5. 07_strings.kark + 08_arrays.kark — the core collections

  6. 09_maps.kark + 10_structs.kark — associative and record data

  7. 11_match.kark + 12_casts.kark — option matching and numeric casts

  8. 13_enums.kark + 14_adt_match.kark — enums and ADT-style match

  9. 15_closures.kark — let-bound closures and variable capture

Match (excerpt)

From 11_match.kark — literal-bound arms and Some/None:

func triple(x) {
    return match x {
        Some(v) => v * 3
        None => 0
    }
}

Note

For computed values, prefer exhaustive literal arms: wildcard-match behavior is not yet reliable on both engines.

See also

Algorithms — more recursive programs with expected output. Karkain Language Guide — the language guide these examples exercise. First Program — the guided first program.