Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
32
Task/Anonymous-recursion/Ruby/anonymous-recursion-7.rb
Normal file
32
Task/Anonymous-recursion/Ruby/anonymous-recursion-7.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
require 'continuation' unless defined? Continuation
|
||||
|
||||
module Kernel
|
||||
module_function
|
||||
|
||||
def function(&block)
|
||||
f = (proc do |*args|
|
||||
(class << args; self; end).class_eval do
|
||||
define_method(:callee) { f }
|
||||
end
|
||||
ret = nil
|
||||
cont = catch(:function) { ret = block.call(*args); nil }
|
||||
cont[args] if cont
|
||||
ret
|
||||
end)
|
||||
end
|
||||
|
||||
def arguments
|
||||
callcc { |cont| throw(:function, cont) }
|
||||
end
|
||||
end
|
||||
|
||||
def fib(n)
|
||||
raise RangeError, "fib of negative" if n < 0
|
||||
function { |m|
|
||||
if m < 2
|
||||
m
|
||||
else
|
||||
arguments.callee[m - 1] + arguments.callee[m - 2]
|
||||
end
|
||||
}[n]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue