Bob is word-heavier than you might expect from a coding language. In Bob you don't need to end each line with a semicolon, you don't have to stuggle with parenthesis or punctuation as much as you normally do. You can write Bob in every editor as you don't necessarily need auto-completion, auto-bracketing and all that stuff to make your life easier because it is already as easy as it gets!
In Bob, all you need to care about is the right grammar. With that, we literally mean grammar as you know it from spoken languages. Thus, Bob is unfortunately not Yoda-friendly.
To start off, I will show you a few examples. Not to make you nervous, but to show you how easy it is to code something in Bob. You will see that Bob is similar to spoken language and doesn't require much awareness about syntax.
This simple code would print hello world!
to the console.
say 'hello world!'
Here we create an object with some key-value pairs and then show it as a table in the console. This functions is very useful when you want to keep track how the value of variables changes.
let object have
a: 100
b: 200
c: 200
list object
In this example we assign a value to the variable foo
. An if-condition with a statement follows. Inside, we multiply the vaiable by 3
and print it to the console.
let foo be 100
if foo is 100
foo * 3
say foo
Here we create an array that has a few values in it. Because we are lazy to enter each value, we can give a range of values by a starting value, an ending value and a gap value. In this example, array
would get the values 100, 150, 200, 250, 300
.
Afterwards we loop over the array and check if the current value is a multiple of 100
. If yes, we print it to the console. In our case, Bob will just print 100
, 200
and 300
.
let array have
100 to 300 in 50
for each i in array
if array|i| fits 100
say array|i|
If we want to run over a range of values, we can do so with the following type of loop. In this setup, it will run from 50
to 100
in steps of 1
. Inside, a condition checks if the current value is a multiple of 5
. If yes, Bob will show it in the console.
let n be 100
loop i from n/2 to n
if i fits 5
say i
Lets first create an array we want to sort.
let array have
4, 54, 1, 32, 18, 24, 7
As we will need to swap places of some of the array's values, we can initialize a separate function for that.
init swap with arr, i, j
let tmp be arr|i|
arr|i| be arr|j|
arr|j| be tmp
Now that we have that, we can finally get to the actual algorithm.
init bubbleSort with arr
let swapped
while swapped is true
swapped be false
for each i in arr
if arr|i| and arr|i+1| and arr|i| > arr|i+1|
swap with arr, i, i+1
swapped be true
throw arr
To sort the array we've created in the beginning and print it to the console, simply write:
say bubbleSort with array