Here are example using Xper to do a few simple programming problem and explain xper concept
Classic Hello World
world program not much to talk about it exactly like python
or other language Hello World
print("Hello World")
FizzBuzz
is old and cliché introducing FizzBuzzBazz
, loop from 1
to 20
and print out Fizz
, Buzz
and Bazz
if the number is divisible by 3
, 5
and 7
Here a normal implemetation:
for (num of 1..=20) {
str = ""
num % 3 == 0 ? str += "Fizz"
num % 5 == 0 ? str += "Buzz"
num % 7 == 0 ? str += "Bazz"
print( str == "" ? num : str)
}
But this is also valid
for (num of 1..=20) {
str = "" + {num % 3 == 0 ? "Fizz" : ""} + {num % 5 == 0 ? "Buzz" : ""} + {num % 7 == 0 ? "Bazz" : ""}
print(str == "" ? num : str)
}
Classic Fibonacci
implemetation both iteratively and recursively.
fib := (n) => {
out = [0; 1]
for (i of ..n) out[-1] + out[-2] >> out
}
fib(5)
fib := (n) => n < 2 ? n : fib(n - 1) + fib(n - 2)
fib(10)
isPalindrome := (num) => str(num) == -str(num)
print(
isPalindrome(1234)
isPalindrome(121)
isPalindrome(121)
)
Consider the following code
a = 10
for (,,) {
a <= 0 ? break : a--
print(a)
}
This would never break because the break signal is not carry out to the main loop
a = 10
for (,,) {
a <= 0 ? break# : a--
print(a)
}