Sampi

Home Older Versions Documentation Changelog Learn


Getting Started

Go back to the home tab, and click DOWNLOAD. Then download the version that works on your operating system. Open the file. You might need to tell your computer that the file is safe to run. Now you are ready to use Sampi!


Coding in Sampi

This is a program that should print "Hello, World!" in the lower box of Sampi's IDE.


say("Hello, World!")
        

Variables

This creates a variable.


set x to 1
say(x)
        

A variable is like a box that stores a value. Right now, there is a box called x with the value 1 in it. You can change variables too.


set x to 1
say(x)
set x to x + 1
say(x)
        

When naming a variable in Sampi, there are some rules. It can have only lower case letters, upper case letters, numbers, and underscores (_). It cannot be one of Sampi's keywords. Keywords are just special words that do things. When naming a variable in Sampi, we ask you to use snake_case. That is when you use underscores instead of spaces, like player_x.


Glue

Sometimes when making a variable, you don't want it to change. It is not really a variable if it can't change, but it is useful for stuff like constants (pi, e) and the name of a player. This is when you use the glue keyword. The glue keyword means you can't change its value.


glue pi to 3.14
        

Glueing variables has some rules. Once a value is glued it can never be changed. Also, you cannot glue a variable that has already been created.


If Statements

Right now, the computer will do the same thing each time the program runs. Programming is much more powerful when it can make decisions. For that, programmers use if statements. An if statement can do different things depending on if a condition is true or false.


set x to 1

if x = 1:
    say("x = 1!")
end
        

Now if x is anything other than 1, it won't say anything! But if you don't change the code directly, it will still always say the same thing! This is why we need user input. The ask() command allows the computer to get information from you.


set colour to ask("What is you favorite colour? ")

if colour = "green":
    say("That's my favorite too!")
else:
    say("That is a fun colour.")
end
        

If you type "green", it says, "That's my favorite too!", but if you type anything else, it says, "That is a fun colour."! The same program is now giving different outputs! The else tells the computer: If the condition is not true, then do this. You can also chain if and else together to make more complex decisions.


set num to number(ask("Give me a number. "))

if num > 0:
    say("Your number is positive.")
else if num < 0:
    say("Your number is negitave")
else:
    say("Your number is zero.")
end
        

number(ask("Give me a number. ")) turns what you type from a string to a number. A string is text, like "hello", or "world", and number is a value like 1, 5.7, or 3. Since "4" is a string (see the double quotes), it need to be coverted to a number to be compared with 0. This also works the other way:


string(3)
        

will make 3 into "3".


Math

Sampi can also do math.


OperationExampleResult
Addition1 + 12
Subtraction3 - 12
Multiplication2 * 48
Division8 / 24
Exponentiation3^29
Negation~3-3

Math in Sampi is strange. ~3 gives -3. You can't simply use -3. The biggest change is the loss of the order of operations. Normally, 2 + 3 * 4 will return 14 (2 + (3 * 4)), but in Sampi, it just goes left to right, so 2 + 3 * 4 is 20 ((2 + 3) * 4). This is very important when chaining conditions. These are the comparisons and how to chain them.


ComparisonExampleResult
Equal1 = 1true
Greater than2 > 1true
Less than2 < 3true
Greater than or equal to1 >= 1true
Less than or equal to1 <= 2true
Not equal1 != 2true

OperationExampleResult
Andtrue and falsefalse
Ortrue or falsetrue
Notnot truefalse

Condition chaining is when left to right evaluation can be annoying. If you type:


say(1 = 1 and 2 = 2)
        

You will get an error! But why? It is because instead of doing it like this: (1 = 1) and (2 = 2), it evaluates it this way: ((1 = 1) and 2) = 2. It tries to see if 1 = 1 and 2, which is invalid. The fix is to run:


say(1 = 1 and (2 = 2))
        

Now it evaluates the code: (1 = 1) and (2 = 2), which is what we want! When chaining comparisons, always use parentheses.


Loops

Suppose you want to say "Hello!" 5 times? Simple! Just run:


say("Hello!")
say("Hello!")
say("Hello!")
say("Hello!")
say("Hello!")
        

OK, but what if you want it to run 1,000 times? There is a better option. This is where you would use a loop. A loop does what it says. It repeats code a known number of times.


repeat 5:
    say("hello")
end
        

If you want to know what iteration you are on, use "as" and the name of the variable you want. It is common to use the letter i, short for iterator.


repeat 5 as i:
    say(i)
end
        

What if you want to repeat code an unknown number of times? That is where until loops come in. They repeat code until a condition is true. Here is a program where you guess the password:


set guess to ""

until guess = "password"
    say("That is not the password.")
    set guess to ask("What is the password? ")
end

say("You found the password!")
        

The code will repeat until guess = "password". With until loops, you can make an infinite loop. "until true" will never end. This can be useful, but be careful.


Functions

What if you are writing a program, and you keep typing say("hello"). That can get annoying. Luckily, this is why there are Functions. Functions are reusable code. You start a function with func, and call it with parentheses.


func greet:
    say("Hello")
end

greet()
        

But what if you want to greet different people? That is what arguments are for. They allow you to give functions information.


func greet name:
    say("Hello, " + name + "!")
end

greet("Alice")
greet("Bob")
        

Now you can greet new people! But what if you what to get a value back from the function? That is what the return keyword is for.


func square x:
    return x * x
end

say(square(2))
        

Lists and Dictionaries

If variables are like a box, then lists are like a box split up into little compartments. You can tell it which compartment you want to store a value in, and which compartment you want to get a value from. The compartments are numbered 1, 2, 3, 4, and so on. Sampi lists use 1 based indexing. You can create a list with square brackets ([]).


set x to ["a", "b", "c"]
say(x[1])
        

To get how long a list is, use length().


say(length([1, 2, 3]))
        

length() works on strings, dictionaries, and numbers as well. A dictionary is, well, a dictionary! It is like a list, but instead of using 1, 2, 3, 4, and so on, it uses any label you want! You can make a dictionary using curly braces ({})


set x to {"a":1, "b":2}
say(x["a"])
        

You can edit the contents of lists and dictionaries using somthing called a method. A method is a special command that you can run on a specific value.


set x to ["a", "b", "c"]
        

MethodExampleResult
add()x.add("d")["a", "b", "c", "d"]
remove()x.remove("b")["a", "c"]
delete()x.delete(3)["a", "b"]
get_index()x.get_index("c")3
insert()x.insert(1, "hello")["a", "hello", "b", "c"]
clear()x.clear()[]
join()x.join(", ")"a, b, c"


set x to {"a": 1, "b": 2, "c": 3}
        

MethodExampleResult
add()x.add("d", 4){"a": 1, "b": 2, "c": 3, "d": 4}
remove()x.remove("b"){"a": 1, "c": 3}
clear()x.clear(){}

Somtimes, you want to loop through the contents of a list. This can be useful for saying every item or to change every item, like to double them. Just make a repeat loop, but use a list instead of a number.


letters = ["a", "b", "c", "d"]

repeat letters as letter:
        say(letter)
end