So far in Setanta we’ve only been using text and numbers in our programs. These have allowed us to do some powerful stuff, but they are also very limiting.
What if we want to deal with more than one piece of text, or more than one number. That’s where
Lists are created with [, ]
) around the outside and commas (,
) to [100, 200, 300]
is a list with 3 elements. 100 is the first element, 200 is the second, and 300 is the last element. []
is an empty list.
Try adding 4
as a fourth element to the list [1, 2, 3]
here:
In that example
Lists can be +
” operator, just like text and numbers.
Using +
with two lists creates a
We can use this to add elements to the
[1, 2, 3]
, and put it in the new variable “x
”.x
with the value x + [4]
. The value stored in x
is [1, 2, 3]
, so this evaluates to [1, 2, 3] + [4]
which equals [1, 2, 3, 4]
. We then update the variable x
to this value.x
, which is now [1, 2, 3, 4]
.Now that we know how to make lists, and how to put values into the lists, how do we get values back out?
The
We use square brackets ([]
) again to access an element at a specific index. We wrap the index in square brackets and place it our_list
, we can access the first element by writing our_list[0]
, the second element by writing our_list[1]
, and so on. Here’s an example you can play with:
Try changing the 0
on line 5 to a different number and see what happens.
Our list [10, 20, 30, 40]
in the last example is only 4 elements
You should get an
If a list has some
We often want to know our_list
, we write fad@our_list
to get the length. Try it out!:
(
This can be very useful if we want to get the fad@our_list - 1
. Try it out here:
Text can be thought of as a list of
E.g.
Now that we have our lists, what can we do with them. The main tool that we will use with lists are called “
Loops give us a way of taking some piece of code and repeating it over and over. We’ll take a look at
Our first type of loop we’re going to look at is called the “
The syntax for a “le idir” loop is:
How a “le idir” loop works is:
{ }
).This might be a little confusing to read, but it’s easy to see with an example. Let’s run the following code:
Running this code you should see that it writes “0”, then “1”, then “2”, “3”, and “4”. It then writes “
To execute the “le idir” loop, Setanta creates a new variable called “i
”. We could choose any name for this variable, but in our example we chose “i
”.
Setanta first assigns the start value (in our case 0
) to i
, and then runs the code in the curly braces. This code is scríobh(i)
. So the first thing that gets written is “0”.
Next Setanta increases the value of i
, so now i
contains the value 1
. It then checks, is i
equal to the finish value (in our case 5
)? It isn’t, so it returns to step 3 and runs the code scríobh(i)
. Now however i
is 1
, so “1” is written on the console.
This continues for 2, 3 and 4.
Finally after Setanta runs the code for 4
it increases the value of i
to 5
. Now however it checks if i
is equal to the finish value, and this time it is. This tells Setanta that we are finished, so it exits the loop and continues on to the next statement.
The next statement is scríobh("Finished")
, so it writes “Finished” on the console. This is the last statement so the program finishes.
Now we will see how to use “le-idir” loops with lists. We will use a loop to print out every element of a list.
Let’s start with a short list:
There are five elements in that list, we want to use each one, because of that we need to use every index. We should write my_list[0]
, my_list[1]
, my_list[2]
, my_list[3]
and my_list[4]
. To do that, we should use a loop to go from 0 to 4. We saw in the previous example that we can use this list to go from 0 to 4:
Let’s use that again to write every element of the list, try it out:
But now, what happens when we add a new number to the list? There is now a new number 6
in the list, try the code again:
It doesn’t write out “6” on the console! Why? It doesn’t write it because the loop stops when it gets to index 4, and 6
is at index 5. If we want to we can change the finish value from 5
to 6
, but is there a smarter way?
Notice that in the first example that 5
is equal to the length of the list, this is not a coincidence! We want the loop to go from 0 to the last index, and we saw before that the last index would be fad@my_list - 1
. A “le-idir” loop stops before the finish value", because of that if we use fad@my_list
as the finish value, the loop will stop at fad@my_list - 1
. This is exactly what we want to do!
Now this is our code:
When you run this now it writes every element of the list! Try out shorter lists and longer lists!
Let’s explore how we could
>-- Start with some list
my_list := [11, 12, 13, 14, 15]
>-- Use le idir loop to loop over the elements
le i idir (0, fad@my_list) {
}
Now we can make a new variable to store the
>-- Start with some list
my_list := [11, 12, 13, 14, 15]
>-- A variable to store the sum of the elements.
sum := 0
>-- Use le idir loop to loop over the elements
le i idir (0, fad@my_list) {
}
Then for each element of the list we can add it’s value to the sum with sum = sum + my_list[i]
>-- Start with some list
my_list := [11, 12, 13, 14, 15]
>-- A variable to store the sum of the elements.
sum := 0
>-- Use le idir loop to loop over the elements
le i idir (0, fad@my_list) {
sum = sum + my_list[i]
}
Now we just add our final line scríobh(sum)
to write the sum of the list on the console. Try out the final code!
We used sum = sum + my_list[i]
to add my_list[i]
to the sum variable. Setanta has a shorthand for this common problem, the +=
operator. We can just write sum += my_list[i]
to add my_list[i]
to the sum.
There is also *=
, -=
and /=
for
A le idir loop can also accept a third number, positioned after the start and finish values. This value
For example:
This loop starts at 0 and increases by 2 instead of 1 each time.
If the finish value is smaller than the start value, the loop will
Let’s combine what we’ve seen before with our
There are a few new actions and values we’ll have to look at before we start.
First let’s see how do we generate a random number? Lucky for us Setanta has 2 actions that can do that for us, rand
and slánuimh_rand
. These are maths actions so we access them with rand@mata
and slánuimh_rand@mata
. (Once again, we’ll see what the @ does in the future).
rand
returns a random number between 0 and 1, not necessarily a slánuimh_rand
takes two arguments and returns a random slánuimh_rand(3, 5)
will return one of 3, 4 or 5 with equal The names of these actions are rand
and slánuimh_rand
because “rand” is short for “randamach” meaning “
Try out this code a few times:
You should get different numbers from 0 to 100 each time.
If we want to place circles randomly on the stage, we’ll need to know how big it is.
We can access the width and height by accessing the values fad_x@stáitse
and fad_y@stáitse
.
Try running this code:
If you resize your window you might get different results.
Ok let’s write our program: First we want to ask the user how many circles they want. ceist
to ask for user input, and go_uimh
to convert text to numbers.
Now we would like to create that many circles, so we would like to repeat some code n_circles
times. We can use a le idir loop for this
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
>-- We'll put our circle logic here
}
(We’ve named our loop variable i
, but we won’t be using it in the loop)
To draw a circle randomly, we want to pick a random X coordinate, a random Y coordinate, and a random radius. We can uses fad_x
and fad_y
to get the slánuimh_rand
to generate a random number in that range by using slánuimh_rand@mata(0, fad_x@stáitse)
and slánuimh_rand@mata(0, fad_y@stáitse)
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
randx := slánuimh_rand@mata(0, fad_x@stáitse)
randy := slánuimh_rand@mata(0, fad_y@stáitse)
}
We’ll just use 50 to 100 as a range for our radius, as we don’t want them
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
randx := slánuimh_rand@mata(0, fad_x@stáitse)
randy := slánuimh_rand@mata(0, fad_y@stáitse)
randr := slánuimh_rand@mata(50, 100)
}
Now that we’ve picked random coordinates and size, all that’s left is to draw the circles using ciorcal@stáitse
.
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
randx := slánuimh_rand@mata(0, fad_x@stáitse)
randy := slánuimh_rand@mata(0, fad_y@stáitse)
randr := slánuimh_rand@mata(50, 100)
ciorcal@stáitse(randx, randy, randr)
}
Try running the code! You’ll have to type in how many circles you want, then switch tabs to the stage to see the results.
It’s a bit boring having all the circles be the same colour. Let’s switch it up using lists!
Let’s make a list of colours we’d like to see and put it at the top of the program.
colours := ["dearg", "buí", "gorm", "glas"]
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
randx := slánuimh_rand@mata(0, fad_x@stáitse)
randy := slánuimh_rand@mata(0, fad_y@stáitse)
randr := slánuimh_rand@mata(50, 100)
ciorcal@stáitse(randx, randy, randr)
}
I chose “dearg”, “buí”, “gorm” and “glas”. Now when before we draw a circle, we can switch to a random colour from our list.
To pick a random colour we want to pick a random element from the list. We can do this by picking a random index using slánuimh_rand
, we want any index between the first (0) and the last (fad@colours - 1
). This gives us index := slánuimh_rand@mata(0, fad@colours - 1)
.
colours := ["dearg", "buí", "gorm", "glas"]
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
randx := slánuimh_rand@mata(0, fad_x@stáitse)
randy := slánuimh_rand@mata(0, fad_y@stáitse)
randr := slánuimh_rand@mata(50, 100)
rand_index := slánuimh_rand@mata(0, fad@colours - 1)
ciorcal@stáitse(randx, randy, randr)
}
We can use the index now to pick our random colour “colours[rand_index]
” and use dath@stáitse
to switch to that colour:
colours := ["dearg", "buí", "gorm", "glas"]
n_circles := go_uimh(ceist("How many circles? "))
le i idir (0, n_circles) {
randx := slánuimh_rand@mata(0, fad_x@stáitse)
randy := slánuimh_rand@mata(0, fad_y@stáitse)
randr := slánuimh_rand@mata(50, 100)
rand_index := slánuimh_rand@mata(0, fad@colours - 1)
dath@stáitse(colours[rand_index])
ciorcal@stáitse(randx, randy, randr)
}
Now try running the code!
Switch to using ciorcal_lán@stáitse
instead of ciorcal
, what happens?
(Hint: “