aboutsummaryrefslogtreecommitdiff
path: root/entries/omentic/nim
diff options
context:
space:
mode:
Diffstat (limited to 'entries/omentic/nim')
-rw-r--r--entries/omentic/nim/fib.nim28
1 files changed, 27 insertions, 1 deletions
diff --git a/entries/omentic/nim/fib.nim b/entries/omentic/nim/fib.nim
index a12f8e6..7552e42 100644
--- a/entries/omentic/nim/fib.nim
+++ b/entries/omentic/nim/fib.nim
@@ -1,12 +1,15 @@
+## standard
func fib(n: Natural): Natural =
if n < 2:
return n
else:
return fib(n-1) + fib(n-2)
+## tail-recursive
func fib2(n: int, a = 0, b = 1): int =
- return if n == 0: a else: fib2(n-1, b, a+b)
+ if n == 0: a else: fib2(n-1, b, a+b)
+## iterable
iterator fib3: int =
var a = 0
var b = 1
@@ -15,3 +18,26 @@ iterator fib3: int =
swap a, b
b += a
+## compile-time, list stuff
+import std/sugar
+static:
+ proc scan[T, U](list: var seq[T], first, second: U, fun: (U, U) -> U) =
+ var prev = (first, second)
+ for i, elem in list:
+ list[i] = fun(prev[0], prev[1])
+ prev[0] = prev[1]
+ prev[1] = list[i]
+
+ proc fib4(n: int): seq[int] =
+ result = newSeq[int](n)
+ result.scan(0, 1, (p, c) => p + c)
+
+## stupid. macro expansions, outputs (0+1)+((0+1)+(0+1)) etc
+import std/macros
+macro fib5(n: static[int]): untyped =
+ var a = quote do: 0
+ var b = quote do: 1
+ for _ in 0 ..< n:
+ swap a, b
+ b = infix(a, "+", b)
+ return a