Skip to content

Examples

Frederik Tobner edited this page Dec 17, 2022 · 21 revisions

Hello World:

printf("Hello World\n");

Greets the user

var name = readLine();
printf("Hello {}\n", name);

Function that reverts a string

fun revstr(src) {
    var reversed = "";
    for(var i = 0; i < strlen(src); i += 1)
        reversed += src[strlen(src) - i - 1];
    return reversed;
}

var example = "xollec";
printf("{}\n", revstr(example));

Print the first 10 Fibonacci Numbers:

fun fibonacci() {
	var x = 0;
	var temp = 0;
	var b = 1;
	fun number() {
		temp = x;
		x = b;
		b = temp + x;
		return temp;
	}
	return number;
}

var fibo = fibonacci();
for (var j = 0; j < 10; j += 1)
	printf("{}\n", fibo());

Simple class representing a rectangle:

class Rectangle {
	init(x, y) {
	        this.x = x;
		this.y = y;
	}
	area() {
		return this.x * this.y * 0.5;
	}
}  

Single Linked List

class Node {
	init(value, nextNode) {
		this.value = value;
		this.nextNode = nextNode;
	}
}

class SingleLinkedList {
	init() {
		this.head = null;
	}

	addLast(value) {
		if(!this.head) {
			this.head = Node(value, null);
			return;
		}
		var current = this.head;
		while(current.nextNode)
			current = current.nextNode;
		current.nextNode = Node(value, null);
	}

	addFirst(value) {
		this.head = Node(value, this.head);
	}

	printList() {
		var current = this.head;
		while(current) {	
			printf("{}\n", current.value);		
			current = current.nextNode;
		}
	}

	remove(value) {
		var current = this.head;
		var prevoius = null;
		while(current) {	
			if(current.value == value) {
				if(prevoius)
					prevoius.nextNode = current.nextNode;
				else
					this.head = current.nextNode;
			}
			else {
				prevoius = current;		
			}
			current = current.nextNode;
		}
	}
}

var list = SingleLinkedList();
list.addLast(random());
list.addLast(random());
list.addLast(random());
list.addLast(1);
list.addLast(1);
list.addFirst(1);
list.printList();
list.remove(1);
list.printList();

Binary Search Tree

class Node {
    init(value, left, right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}
class BinarySearchTree {
    init() {
        this.rootNode = null;
    }

    add(x) {
        if(!this.rootNode) {
            this.rootNode = Node(x, null, null);
            return;
        }
        var node = this.rootNode;
        for(;;) {
            if(node.value == x) return;
            if(node.value > x) {
                if(!node.left) {
                    node.left = Node(x, null, null);
                    return;
                }
                node = node.left;            
            }
            else {
                if(!node.right) {
                    node.right = Node(x, null, null);
                    return;
                }
                node = node.right;            
            }
        }
    }

    // Prints the values from a specific node and all the nodes below
    printFromNode(node) {
        if(node) {
            this.printFromNode(node.left);
            printf("{}\n", node.value);
            this.printFromNode(node.right);
        }
    }

    // Prints the values stored in a binary search tree
    printValues() {
       this.printFromNode(this.rootNode);
    }
}

var bst = BinarySearchTree();
for (var i = 0; i < 10; i += 1) 
    bst.add(random() % 100);
bst.printValues();

Simple inheritance example

class Person {
    init(name) {
        this.name = name;
    }
    Greet() {
        printf("Hi, my name is {}\n", this.name);
    }
}

class Student : Person {
    init(name, courseOfStudy, university) {
        super.init(name);
        this.courseOfStudy = courseOfStudy;
        this.university = university;
    }
    Greet() {
        super.Greet();
        printf("I study {} at the {}.\n", this.courseOfStudy, this.university); 
    }
}

var p = Student("Jeff", "computer science", "harvard university");
p.Greet();
Clone this wiki locally