2 ms·
Here it is in Ruby. LISP_METH = /^c[ad]+r$/ def method_missing(name, *args) meth = name.to_s raise NoMethodError, "undefined method '#{name}'" u
by jashkenas 17y ago
Here it is in Ruby.
LISP_METH = /^c[ad]+r$/
def method_missing(name, *args)
meth = name.to_s
raise NoMethodError, "undefined method '#{name}'" unless meth =~ LISP_METH
list = args.first
calls = meth.split('')[1..-2].reverse
calls.each {|call| list = (call == 'a' ? list.first : list[1..-1]) }
list
end
cadr [1,2,3]
# => 2
caaddddr [1,2,3,4,[9,8,7]]
# => 9
cadadr [1,[9,[100],8,7],3,4]
# => 100