Copy · paste · run

Examples you can steal

Every snippet below is real, tested X++ — the expected output shown is produced by the actual native VM. Hit ▶ playground to run it in your browser, or copy it into hello.xp and run x run hello.xp.

basics

Hello, X++

The classic first program — straight from the repo's examples/hello.xp.

RNM=ZITR

out "Hello from X++ v0.3!"
output:  Hello from X++ v0.3!
functionsrecursion

Fibonacci, recursive

The README's poster child. Recursion, if / end and a counting loop in 10 lines.

RNM=ZITR

fn fib(n):
  if n <= 1:
    return n
  end
  return fib(n-1) + fib(n-2)
end

loop i from 0 to 10:
  out i, fib(i)
end
output:  0 0 · 1 1 · 2 1 · … · 10 55
basicsalgorithm

FizzBuzz

The interview classic — with elif chains and modulo.

RNM=ZITR

loop n from 1 to 15:
  if n % 15 == 0:
    out "FizzBuzz"
  elif n % 3 == 0:
    out "Fizz"
  elif n % 5 == 0:
    out "Buzz"
  else:
    out n
  end
end
output:  1 · 2 · Fizz · 4 · Buzz · … · FizzBuzz
algorithmslists

Prime sieve

Sieve of Eratosthenes with a boolean list — push, indexing and nested loops.

RNM=ZITR

limit = 50
is_prime = []
loop i from 0 to limit:
  push true to is_prime
end
is_prime[0] = false
is_prime[1] = false

loop n from 2 to limit:
  if is_prime[n]:
    m = n * n
    while m <= limit:
      is_prime[m] = false
      m = m + n
    end
  end
end

count = 0
loop n from 2 to limit:
  if is_prime[n]:
    out n
    count = count + 1
  end
end
out "primes found:", count
output:  2 · 3 · 5 · … · 47 · primes found: 15
lists & dicts

Collections tour

Lists, push, dicts with dot-access, keys — the everyday toolkit.

RNM=ZITR

nums = [1, 2, 3]
push 4 to nums
out "list:", nums
out "len:", len(nums), "sum:", sum(nums)

user = {"name": "Aagastya", "lang": "X++"}
user.version = "0.4.1"
out user
out "keys:", keys(user)
loop k in keys(user):
  out " ", k, "=", user[k]
end
output:  list: [1, 2, 3, 4] · len: 4 sum: 10 · …
dictsalgorithm

Word counting

The dict pattern you'll use everywhere: contains check → increment or seed.

RNM=ZITR

words = ["xpp", "is", "fast", "xpp", "is", "easy", "xpp", "rocks"]
counts = {}
loop w in words:
  if contains(counts, w):
    counts[w] = counts[w] + 1
  else:
    counts[w] = 1
  end
end

out "word counts:"
loop w in sorted(keys(counts)):
  out " ", w, "x" + str(counts[w])
end
out "unique words:", len(counts)
output:  easy x1 · fast x1 · is x2 · rocks x1 · xpp x3 · unique: 5
safe / failfunctions

Errors that don't kill your program

safe / fail catches runtime errors — even ones thrown deep inside called functions.

RNM=ZITR

safe:
  x = 1 / 0
  out "never reached"
fail e:
  out "caught:", e
end

fn risky(n):
  if n < 0:
    return 0 - n
  end
  return 1 / n
end

loop n in [4, 0, -7]:
  safe:
    out "risky(" + str(n) + ") =", risky(n)
  fail e:
    out "risky(" + str(n) + ") failed:", e
  end
end
output:  caught: division by zero · risky(4) = 0.25 · …
inputgame

Number guessing game

A real REPL game with in and while true. In the playground, put your guesses in the Input box first — try 50, 25, 37.

RNM=ZITR

secret = 37
tries = 0

while true:
  g = int(in "Your guess (1-100): ")
  tries = tries + 1
  if g == secret:
    out "Correct! You got it in", tries, "tries."
    break
  elif g < secret:
    out "Too low!"
  else:
    out "Too high!"
  end
end
output:  Too high! · Too low! · Correct! You got it in 3 tries.
strings

String toolbox

Concatenation, repetition, negative indexing, membership, and looping over a list of words.

RNM=ZITR

s = "X++ makes pseudocode real"
out "len:", len(s)
out "first:", s[0], "last:", s[-1]
out "sorted:", sorted([5, 3, 9, 1])
out "repeat:", "x" + "p" * 3
out "contains:", contains(s, "pseudocode")

loop word in ["write", "pseudocode", "ship", "it"]:
  out word
end
output:  len: 25 · first: X last: l · repeat: xppp · …
loops

Countdown & step

Counting loops are inclusive of both ends; a negative step counts down.

RNM=ZITR

# negative step counts down (inclusive of both ends)
loop i from 10 to 0 step -2:
  out i
end

out "liftoff!"
output:  10 · 8 · 6 · 4 · 2 · 0 · liftoff!
benchmark

Hot loop (1,000,000 iterations)

From the repo's bench/ suite. The web VM takes a moment; native ZJIT does this in ~50 ms.

RNM=ZITR

# The native ZJIT engine does this ~100x faster.
total = 0
loop i from 1 to 1000000:
  total = total + i
end
out "sum 1..1000000 =", total
output:  sum 1..1000000 = 500000500000
functionsiterative

Fibonacci, iterative

examples/fib_fast.xp from the repo — O(n) time, instant even for fib(30) in the browser.

RNM=ZITR

fn fib_fast(n):
  a = 0
  b = 1
  if n == 0:
    return 0
  end
  if n == 1:
    return 1
  end
  loop i from 2 to n:
    c = a + b
    a = b
    b = c
  end
  return b
end

out fib_fast(30)
output:  832040