A elegant and modern systems programming language
Learn Lotus by exploring practical examples. Each example demonstrates key language features and patterns.
The simplest Lotus program:
use "io";
fn int main() {
println("Hello, World!");
ret 0;
}
Compute hashes using the hash module:
use "io";
use "hash";
fn int main() {
const string msg = "Test";
int h1 = djb2(msg);
printf("DJB2('%s'): %d\n", msg, h1);
int h2 = fnv1a(msg, 4);
printf("FNV-1a('%s'): %d\n", msg, h2);
int h3 = crc32(msg, 4);
printf("CRC32('%s'): %d\n", msg, h3);
int h4 = murmur(msg, 4, 42);
printf("Murmur3('%s', seed=42): %d\n", msg, h4);
ret 0;
}
Use dynamic arrays from the collections module:
use "io";
use "collections";
fn int main() {
int* arr = array_int_new(8);
array_int_push(arr, 10);
array_int_push(arr, 20);
int n = array_int_len(arr);
printf("Array length: %d\n", n);
ret 0;
}
A minimal TCP connect using the net module:
use "net";
fn int main() {
int fd = socket(2, 1, 0); // AF_INET, SOCK_STREAM
int ok = connect_ipv4(fd, 0x7F000001, 8080); // 127.0.0.1:8080
if ok == 0 {
send(fd, "hi", 2);
close(fd);
}
ret 0;
}
Functions with parameters and return values:
use "io";
fn int factorial(int n) {
if n <= 1 {
ret 1;
}
ret n * factorial(n - 1);
}
fn int main() {
int result = factorial(5);
printf("5! = %d\n", result);
ret 0;
}
Compute maximum and absolute values:
use "io";
use "math";
fn int main() {
int a = -10;
int b = 20;
int magnitude = abs(a);
int larger = max(magnitude, b);
printf("abs(%d) = %d\n", a, magnitude);
printf("max(%d, %d) = %d\n", magnitude, b, larger);
ret 0;
}
Using the string module:
use "io";
use "str";
fn int main() {
string name = "Lotus";
int length = len(name);
printf("Language: %s\n", name);
printf("Length: %d\n", length);
ret 0;
}
Demonstrating different printf verbs:
use "io";
fn int main() {
int value = 42;
printf("Decimal: %d\n", value);
printf("Hex: %x\n", value);
printf("Octal: %o\n", value);
printf("Binary: %b\n", value);
printf("Character: %c\n", 65);
printf("String: %s\n", "Lotus");
ret 0;
}
Control flow in Lotus:
use "io";
fn int main() {
// For loop
printf("Counting up:\n");
for int i = 1; i <= 5; i = i + 1 {
printf("%d ", i);
}
println("");
// While loop
printf("Countdown:\n");
int count = 5;
while count > 0 {
printf("%d ", count);
count = count - 1;
}
println("");
// If/else
int age = 18;
if age >= 18 {
println("Adult");
} else {
println("Minor");
}
ret 0;
}
Dynamic memory allocation with malloc/free:
use "io";
use "mem";
fn int main() {
int size = 10;
int* buffer = malloc(sizeof(int) * size);
if buffer != null {
printf("Allocated %d bytes\n", sizeof(int) * size);
free(buffer);
println("Memory freed");
}
ret 0;
}
Using constants and variables together:
use "io";
const int MAX_ATTEMPTS = 3;
const string APP_NAME = "MyApp";
fn int main() {
int attempts = 0;
string status = "running";
printf("App: %s\n", APP_NAME);
printf("Max attempts: %d\n", MAX_ATTEMPTS);
printf("Status: %s\n", status);
while attempts < MAX_ATTEMPTS {
printf("Attempt %d\n", attempts + 1);
attempts = attempts + 1;
}
ret 0;
}
For comprehensive documentation and API references: