-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFACT_stdlib.ft
102 lines (86 loc) · 1.68 KB
/
FACT_stdlib.ft
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const cat (num op1, num op2) # Concatinate two strings
{
if (size (op1) == 0)
arr_size := 1;
else
arr_size := size (op1);
if (size (op2) == 0)
arr_size += 1;
else
arr_size += size (op2);
num [arr_size] res;
num i;
if (size (op1) == 0) {
res[i] = op1;
i += 1;
} else {
for (i = 0; i < size (op1); i += 1)
res[i] = op1[i];
}
if (size (op2) == 0)
res[i] = op2;
else {
for (num j = 0; j < size (op2); j += 1)
res[i + j] = op2[j];
}
return res;
}
const linked_list (num size)
{
scope main;
scope curr = main;
for (num i = 0; i < size; i += 1) {
(curr:num n) = i;
curr: scope get_end @ ()
{
scope curr = up;
while (curr:next?)
curr = curr:next;
return curr;
}
curr: scope next;
curr = curr:next;
}
return main;
}
# These two are sort of clever.
const is_num (?n)
{
catch n = 0; handle return 0;
return 1;
}
const is_scope (?n)
{
catch n = 0; handle return 1;
return 0;
}
# The value of PI. Haha, just kidding.
const PI = 3.1415926535897932;
const sin (num x)
{
if (x >= PI * 2)
x %= PI * 2;
if (x == 0 || x == PI)
return 0;
else if (x == PI / 2)
return 1;
else if (x == 3 * PI / 2)
return -1;
# We use recursion because that is great.
defunc sin_inner (num n, num x, num lim)
{
if (n >= lim)
return 1;
return n * (n + 1) - (x * x) + (n * (n + 1) * x * x) / sin_inner (n + 2, x, lim);
}
return x / (1 + (x * x) / sin_inner (2, x, 50));
}
# We define the rest of the trig functions in terms of sine
const cos (num x)
{
return sin (PI / 2 - x);
}
const tan (num x)
{
return sin (x) / cos (x);
}