Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / bench_strfib.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Function that recursively allocate and concatenate strings.
16 #
17 # It is related to the Fibonacci sequence since:
18 #
19 # * `strfib(0).length == 0`
20 # * `strfib(1).length == 2`
21 # * `strfib(n).length = strfib(n-1).length + strfib(n-2).length + 2`
22 fun strfib(i: Int): String
23 do
24 if i == 0 then return ""
25 if i == 1 then return "()"
26 return "({strfib(i-2)}{strfib(i-1)})"
27 end
28
29 var quiet = false
30 if args.has("-q") then
31 quiet = true
32 args.remove("-q")
33 end
34
35 var n = 15
36 if args.length > 0 then n = args.first.to_i
37 var res = strfib(n)
38
39 if quiet then
40 print res.length
41 else
42 print res
43 end