-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.j
executable file
·60 lines (47 loc) · 1.03 KB
/
example.j
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
#!/usr/local/bin/jirachi
# loop
fun fib1(n)
# list comprehension: res = [1, 1, 1]
res = for i = 0 to 3 then 1
for i = 3 to n + 1 then
res = res + (res[i - 1] + res[i - 2])
end
return res[n] # can remove the return keyword
end
fib2 = fun(n)
if n <= 2 then
return 1
end
f1 = 1
f2 = 1
f3 = 0
return for i = 3 to n + 1 then # return final expression value
f3 = f1 + f2
f1 = f2
f2 = f3
end
end
# recursion
fib3 = fun(n) # anonymous function
if n <= 2 then
1
else
fib3(n - 1) + fib3(n - 2)
end
end
num2str = fun(number) -> '' + number # lambda
print_fib_values = fun(fib_fun, n) # higher order function
print('fibonacci value is ')
for i = 1 to n + 1 then
res = fib_fun(i)
print(num2str(res) + ' ')
end
println('')
end
while true then
println('please input number:')
n = input_number()
print_fib_values(fib1, n)
print_fib_values(fib2, n)
print_fib_values(fib3, n)
end