fib = lambda do |n|
if n < 1
return 0
elsif n < 2
return 1
else
return fib.call(n-1) + fib.call(n-2)
end
end
def memoize(f)
memo = {}
return lambda do |x|
if memo.key?(x)
return memo[x]
end
memo[x] = f.call(x)
return memo[x]
end
end
fib = memoize(fib)
10.times do |x|
puts fib.call(x + 1)
end
To embed this project on your website, copy the following code and paste it into your website's HTML: