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.
Hello, X++
The classic first program — straight from the repo's examples/hello.xp.
RNM=ZITR out "Hello from X++ v0.3!"
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
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
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
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
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)
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
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
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
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!"
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
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)