Before we move onto
We saw early in the tutorial that Setanta supports addition, multiplication, subtraction and division (using +
, *
, -
and /
). Setanta supports more useful operators and actions to make calculations easier!
Setanta has 2 more operators that we haven’t seen yet. They are the “modulo” %
), and the integer division operator (//
).
The modulo operator (%) is an operator you might not be familiar with, but it’s extremely useful.
The modulo operator takes two numbers, and returns the
e.g. 7 % 2
is equal to 1, because 7 has remainder 1 when divided by 2. 11 % 4
has remainder 3, because 11 has remainder 3 when divided by 4.
This operator is extremely useful because it allows us to check lots of things.
We can also use %
to check if one number is divisible by another. A number is divisible by another number if it leaves 0 remainder when you divide by it. e.g. 10 % 2
and 10 % 5
are both 0 because 10 is divisible by 2 and 5.
We can use this to check if a number is
>-- Action to check is `x` prime
gníomh is_prime(x) {
>-- Variable to count divisors
divisors := 0
>-- Check all numbers from 1 to x
le i idir (1, x + 1) {
>-- If x % i == 0 then i is a divisor
má x % i == 0 {
>-- Add 1 to divisor count
divisors += 1
}
}
>-- If divisors == 2, then x is prime
>-- If divisors isn't 2, then x not prime
toradh divisors == 2
}
Let’s try it out! Run this code and try some primes and non-primes:
NB: There is a better algorithm than this, can you create it? We don’t have to check if 1
or x
are factors because they always have remainder 0
, but actually we don’t have to check any number greater than √x
, why?
Read this program and think about the list of numbers it writes.
This program writes the remainder of every number between 0 and 15 after division by 5. If you read the sequence of numbers it writes you’ll see the sequence “0”, “1”, “2”, “3”, “4”, and then it starts again at “0” and continues in the same way: “1”, “2”, “3”, “4”, “0” … etc.
If you run that program with a bigger range of numbers, you’ll see the same i % 5
, if you do the same thing with some number n
you’ll get the sequence 0, 1, 2 ... n - 1, 0, 1, 2, ..., n - 1, ...
etc.
We can use this pattern to go through a list again and again, as if it was
Look at this code:
That program write the members of the list, but then it tries to the member at index 4
and it 4
is too big. But, if we use the modulo operator %
to go back around to 0
when we reach the end of the list everything will be ok. The length of the list is equal to 3 so i % fad@list
will always be equal to 0
, 1
or 2
. Try out our new code:
Now the program doesn’t fail when it gets to the end of the list, instead it goes back to the start. Try adding a new
We’ve seen the division operator (/
) in use already. e.g. 10 / 5 == 2
.
The integer division operator (//
) is just like the division operator, except it rounds down after dividing, so you always get a
Try it out here:
rand@mata
and slánuimh_rand@mata
to get
Here is a quick
Name | Description | Example |
---|---|---|
pí |
Pi |
pí@mata |
e |
e constant (2.71828..) | e@mata |
Name | Description | Example |
---|---|---|
fréamh |
Square |
fréamh@mata(4) == 2 |
cearn |
Square x * x ) |
cearn@mata(2) == 4 |
dearbh |
Absolute value | dearbh@mata(-2) == 2 |
eas |
Exponential (e^x ) |
eas@mata(1) == e@mata |
cmhcht |
x^y ) |
cmhcht@mata(2, 4) == 16 |
log |
log@mata(2) |
|
logb |
Logarithm in some base | logb@mata(16, 2) == 4 |
sin |
Sine | sin@mata(pi@mata/2) |
cos |
Cosine | cos@mata(0) |
tan |
Tangent | tan@mata(pi@mata) |
asin |
Inverse sine | asin@mata(0) |
acos |
Inverse cosine | acos@mata(pi@mata) |
atan |
Inverse tangent | atan@mata(0) |
rand |
Random number from 0 to 1 | rand@mata() |
slánuimh_rand |
Random whole number in some range | slánuimh_rand@mata(5, 10) |
The only operations and actions we’ve seen so far with +
to add two pieces of text together, using fad
to get the [ ]
) to access (
There is much more we can do with text though! Let’s look at some useful actions.
Use go_téacs
to
For example go_téacs([1, 2, 3]) == "[1, 2, 3]"
and go_téacs(scríobh) == "< gníomh scríobh >"
athchuir
action to replace parts of text with another piece of text. For example you could replace every
If you remember how we had to use the @
fad
to get the length of the text (fad@"hey" == 3
), we also have to use the @
symbol to use athchuir
.
Check out some
“roinn
action to split up a roinn
with "Setanta, is, fun"
into the list ["Setanta", "is", "fun"]
. Or we could use roinn
with spaces to separate a sentence into a list of
Try it out:
“cuid
action allows us to
e.g. cuid@"hello"(1, 4)
returns "ell"
because the text between index 1 and index 4 is "ell"
go_liosta
(meaning “to list”) is an action that returns a list of the
e.g. go_liosta("Setanta")
returns ["S", "e", "t", "a", "n", "t", "a"]
.
Lists share many of the same actions as text including fad
and cuid
. But they also have the following actions available.
“sórtáil
action sorts a list into
For example: sórtáil@[1, 3, 2]
returns [1, 2, 3]
.
NB: Sorting is done in place. i.e. the list will be changed when sórtáil
is called
“nasc
action can be used to turn a list into text, joining the
For example: nasc@[1, 2, 3](", ")
returns "1, 2, 3"
If you need to