Update and add examples
This commit is contained in:
parent
40a538b73d
commit
74bd9c9582
4 changed files with 79 additions and 2 deletions
3
examples/importing.ang
Normal file
3
examples/importing.ang
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
import "math.ang"
|
||||||
|
|
||||||
|
write(sqrt(2))
|
||||||
51
examples/list.ang
Normal file
51
examples/list.ang
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
|
||||||
|
# Empty list
|
||||||
|
write([])
|
||||||
|
|
||||||
|
# List with items
|
||||||
|
write([3, 1, 4, 1, 5, 9, 2, 6, 5])
|
||||||
|
|
||||||
|
# List with items of different types
|
||||||
|
write(["", "私はかっこいいです。", true, nil, nil, 1, 2])
|
||||||
|
|
||||||
|
a := []
|
||||||
|
|
||||||
|
a = a.append(1)
|
||||||
|
a = a.append(2)
|
||||||
|
|
||||||
|
write(a)
|
||||||
|
|
||||||
|
|
||||||
|
list := []
|
||||||
|
|
||||||
|
n := 0
|
||||||
|
x := 0
|
||||||
|
while n < 100 {
|
||||||
|
x = x + 2*n + 1
|
||||||
|
|
||||||
|
list = list.append(x)
|
||||||
|
n = n + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
write(list)
|
||||||
|
write(list.map(func(a) {
|
||||||
|
return a - 1
|
||||||
|
}))
|
||||||
|
write(list.length())
|
||||||
|
write(list.at(69))
|
||||||
|
|
||||||
|
other := []
|
||||||
|
|
||||||
|
a := 1
|
||||||
|
while a <= 10 {
|
||||||
|
other = other.append(a)
|
||||||
|
a = a + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
sum := other.reduce(func(tot, x) {
|
||||||
|
return tot + x
|
||||||
|
}, 0)
|
||||||
|
|
||||||
|
write(sum)
|
||||||
|
|
||||||
|
assert(sum == a*(a-1)/2)
|
||||||
|
|
@ -71,8 +71,31 @@ func round(x) {
|
||||||
# sin(x)
|
# sin(x)
|
||||||
# x: number; an angle in radians
|
# x: number; an angle in radians
|
||||||
# Get the sine of an angle (in radians). https://en.wikipedia.org/wiki/Sine_and_cosine
|
# Get the sine of an angle (in radians). https://en.wikipedia.org/wiki/Sine_and_cosine
|
||||||
|
# TODO: use hashmap with precomputed values and linear interpolation
|
||||||
func sin(x) {
|
func sin(x) {
|
||||||
# todo
|
f := 1
|
||||||
|
x = mod(x, 2*PI)
|
||||||
|
if x > PI {
|
||||||
|
x = -x
|
||||||
|
f = -1
|
||||||
|
}
|
||||||
|
|
||||||
|
# compute sine with a taylor series mock function of sine (valid between -pi and +pi)
|
||||||
|
tot := x
|
||||||
|
l := 1
|
||||||
|
i := 1
|
||||||
|
s := -1
|
||||||
|
|
||||||
|
while i <= 19 {
|
||||||
|
i = i + 2
|
||||||
|
l = s * l * x / i / (i-1)
|
||||||
|
|
||||||
|
tot = tot + l
|
||||||
|
|
||||||
|
s = -s
|
||||||
|
}
|
||||||
|
|
||||||
|
return tot*f
|
||||||
}
|
}
|
||||||
|
|
||||||
# cos(x)
|
# cos(x)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
# see: https://en.wikipedia.org/wiki/Basel_problem
|
# see: https://en.wikipedia.org/wiki/Basel_problem
|
||||||
|
|
||||||
# The amount of terms
|
# The amount of terms
|
||||||
terms := 100000000
|
terms := 1000000
|
||||||
|
|
||||||
# The running sum of terms
|
# The running sum of terms
|
||||||
tot := 0
|
tot := 0
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue