capitalex zrewidował ten Gist . Przejdź do rewizji
Brak zmian
capitalex zrewidował ten Gist . Przejdź do rewizji
1 file changed, 155 insertions
novaweb.nv(stworzono plik)
@@ -0,0 +1,155 @@ | |||
1 | + | Let's write a program that computers the fibonacci sequence. First we must define the base cases of the function. for the sake of simplicity, n <= 0 returns zero. | |
2 | + | ||
3 | + | The base case for n <= 0: | |
4 | + | if n <= 0 then | |
5 | + | return 0 | |
6 | + | end | |
7 | + | ||
8 | + | The base case for 1: | |
9 | + | if n == 1 then | |
10 | + | return 1 | |
11 | + | end | |
12 | + | ||
13 | + | Now for a recursive case. For this implementation, we will stick with the original formula rather than optimizing it. | |
14 | + | ||
15 | + | The recursive case for the fibonacci sequence: | |
16 | + | return fib(n - 1) + fib(n - 2) | |
17 | + | ||
18 | + | Finally, lets assemble this into a Lua function. | |
19 | + | ||
20 | + | The fibonacci function: | |
21 | + | local function fib(n) | |
22 | + | @ The base case for n <= 0 | |
23 | + | @ The base case for 1 | |
24 | + | @ The recursive case for the fibonacci sequence | |
25 | + | end | |
26 | + | ||
27 | + | our program: | |
28 | + | @ The fibonacci function | |
29 | + | print(fib(10)) | |
30 | + | ||
31 | + | do: | |
32 | + | compile | |
33 | + | ||
34 | + | source: | |
35 | + | @ our program | |
36 | + | ||
37 | + | --- | |
38 | + | do: | |
39 | + | %action | |
40 | + | --- | |
41 | + | %action | |
42 | + | ||
43 | + | --- | |
44 | + | dereference | |
45 | + | from: | |
46 | + | %reference | |
47 | + | %reference: | |
48 | + | %code | |
49 | + | --- | |
50 | + | dereference | |
51 | + | from: | |
52 | + | %reference | |
53 | + | spent: | |
54 | + | %code | |
55 | + | compiled code: | |
56 | + | %code | |
57 | + | ||
58 | + | --- | |
59 | + | dereference | |
60 | + | from: | |
61 | + | %reference | |
62 | + | --- | |
63 | + | restore dereference | |
64 | + | from: | |
65 | + | %reference | |
66 | + | ||
67 | + | --- | |
68 | + | restore dereference | |
69 | + | from: | |
70 | + | %reference | |
71 | + | spent: | |
72 | + | %code | |
73 | + | --- | |
74 | + | restore dereference | |
75 | + | from: | |
76 | + | %reference | |
77 | + | %reference: | |
78 | + | %code | |
79 | + | ||
80 | + | --- | |
81 | + | restore dereference | |
82 | + | from: | |
83 | + | %reference | |
84 | + | --- | |
85 | + | compile | |
86 | + | ||
87 | + | ||
88 | + | --- | |
89 | + | compile | |
90 | + | source: | |
91 | + | @ %reference | |
92 | + | --- | |
93 | + | dereference | |
94 | + | from: | |
95 | + | %reference | |
96 | + | needs another pass: | |
97 | + | yes | |
98 | + | ||
99 | + | --- | |
100 | + | compile | |
101 | + | source: | |
102 | + | %code | |
103 | + | --- | |
104 | + | compile | |
105 | + | compiled code: | |
106 | + | %code | |
107 | + | ||
108 | + | --- | |
109 | + | compile | |
110 | + | needs another pass: | |
111 | + | yes | |
112 | + | --- | |
113 | + | next pass | |
114 | + | ||
115 | + | --- | |
116 | + | compile | |
117 | + | --- | |
118 | + | compilation finished | |
119 | + | ||
120 | + | --- | |
121 | + | next pass | |
122 | + | compiled code: | |
123 | + | %code | |
124 | + | --- | |
125 | + | next pass | |
126 | + | source: | |
127 | + | %code | |
128 | + | ||
129 | + | --- | |
130 | + | next pass | |
131 | + | --- | |
132 | + | compile | |
133 | + | ||
134 | + | --- | |
135 | + | compilation finished | |
136 | + | compiled code: | |
137 | + | %code | |
138 | + | --- | |
139 | + | compilation finished | |
140 | + | out: | |
141 | + | %code | |
142 | + | ||
143 | + | --- | |
144 | + | compilation finished | |
145 | + | --- | |
146 | + | print code | |
147 | + | ||
148 | + | --- | |
149 | + | print code | |
150 | + | out: | |
151 | + | %tuple | |
152 | + | --- | |
153 | + | print code | |
154 | + | @print: | |
155 | + | %tuple |
Nowsze
Starsze