Skip to content

Instantly share code, notes, and snippets.

@sitaramshelke
Created April 7, 2019 07:40
Show Gist options
  • Save sitaramshelke/46450d3cc7426278f449a7ab1d10c8bb to your computer and use it in GitHub Desktop.
Save sitaramshelke/46450d3cc7426278f449a7ab1d10c8bb to your computer and use it in GitHub Desktop.
Fibanacci assignment from SICP Meetup 1
puts "=========== Iterative ============"
def fib(n)
i = 0
f = 0
s = 1
puts f
puts s
while i < n - 2
t = f
f = s
s = f + t
puts s
i += 1
end
end
fib(20)
puts "=========== Recursive ============"
def fib_rec(n)
if n > 2
f, s = fib_rec(n - 1)
puts f + s
t = f
f = s
s = f + t
[f, s]
else
puts 0
puts 1
[0, 1]
end
end
fib_rec(20)
puts "=========== Lambda calculus ============"
->(l_fib){
->(num) { l_fib.(l_fib, num) }
}.(
->(g, n){
if n > 2
f, s = g.(g, n - 1)
puts f + s
t = f
f = s
s = f + t
[f, s]
else
puts 0
puts 1
[0, 1]
end
}
).(20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment