News for hackers. Stuff that matters.
Re:Is it possible to write c...r in lisp, for any combinatio... by jashkenas on Sunday October 11, 2009 @10:07AM (#874724)
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

There are no comments on this comment yet.