basic for loop + examples -> era3

This commit is contained in:
Neemek 2026-07-13 10:01:13 +02:00
parent dd7af341a3
commit 0bc8b2ef55
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
24 changed files with 222 additions and 201 deletions

View file

@ -1,19 +1,20 @@
write("Bonjour à tout!");
println("Bonjour à tout!")
if 1 == 2 {
# unreachable
println("Wooot?? One does equal 2????")
} else {
write("Hooray! One does not equal 2!");
println("Hooray! One does not equal 2!")
}
for (var n = 1; n < 10; n = n + 1) {
write("Run number " + str(n));
for n in 0..10 {
println("Run number " + str(n))
}
var a = 2;
a := 2
write(3 * a*a + 10 / 3);
println(3 * a*a + 10 / 3)

View file

@ -1,17 +1,8 @@
# calculate fibonacci numbers with a loop
x := 0
n := 1
p := 1
while x < 100 {
f := n + p
p = n
n = f
write(f)
x = x + 1
(a, b) := (1, 0)
for _ in 0..100 {
(a, b) = (a + b, b)
println(a)
}

View file

@ -1,6 +1,6 @@
func sum(a, b) {
return a + b
fn sum(a: int, b: int) -> int {
a + b
}
write(sum(1, 2))
println(sum(1, 2))

View file

@ -1,4 +1,4 @@
func f(x) {
fn f(x) {
return x*x - 4
}

View file

@ -1,13 +1,11 @@
write("Hello world!")
println("Hello world!")
a := 1 + 2
write(a)
println(a)
if a > 2 {
write("Hooray!! a is greater than 2!!!!")
println("Hooray!! a is greater than 2!!!!")
} else {
write("oh nooo!!! a is less than or equal to 2!!!!!!!!!!")
println("oh nooo!!! a is less than or equal to 2!!!!!!!!!!")
}

View file

@ -1,3 +1,3 @@
import "math.ang"
write(sqrt(2))
println(sqrt(2.0))

View file

@ -1,51 +1,47 @@
# Empty list
write([])
println([])
# List with items
write([3, 1, 4, 1, 5, 9, 2, 6, 5])
println([3, 1, 4, 1, 5, 9, 2, 6, 5])
# List with items of different types
write(["", "私はかっこいいです。", true, nil, nil, 1, 2])
println(["", "私はかっこいいです。", true, nil, nil, 1, 2])
a := []
a = a.append(1)
a = a.append(2)
a = a + [1]
a = a + [2]
write(a)
println(a)
list := []
n := 0
x := 0
while n < 100 {
for n in 0..100 {
x = x + 2*n + 1
list = list.append(x)
n = n + 1
list = list + [x]
}
write(list)
write(list.map(func(a) {
println(list)
println(list.map(func(a) {
return a - 1
}))
write(list.length())
write(list.at(69))
println(list.length())
println(list.at(69))
other := []
a := 1
while a <= 10 {
for a in 0..=10 {
other = other.append(a)
a = a + 1
}
sum := other.reduce(func(tot, x) {
return tot + x
}, 0)
write(sum)
println(sum)
assert(sum == a*(a-1)/2)

View file

@ -20,9 +20,10 @@ tot = tot * 6.0
# get the absolute value of a number
fn abs(x: float) -> float {
if x < 0.0 {
return -x
-x
} else {
x
}
return x
}
# calculate an approximation of the square root of tot using

View file

@ -1,16 +1,15 @@
import "math.ang"
func r_x(t) {
return 8*(exp(-t) - t)
fn r_x(t: float) -> float {
return 8.0*(exp(-t) - t)
}
func r_y(t) {
return 5*(exp(-t) - t)
fn r_y(t: float) -> float {
return 5.0*(exp(-t) - t)
}
func r(t) {
return format("(%s, %s)", [r_x(t), r_y(t)])
fn r(t: float) -> (float, float) {
return (r_x(t), r_y(t))
}
write(r(1))
write()
println(r(1.0))