def permutation(length = size, &block)
      raise Erlang::ImproperListError if improper?
      return enum_for(:permutation, length) if not block_given?
      if length == 0
        yield Erlang::Nil
      elsif length == 1
        each { |obj| yield Erlang::Cons.new(obj, Erlang::Nil) }
      elsif not empty?
        if length < size
          tail.permutation(length, &block)
        end

        tail.permutation(length-1) do |p|
          0.upto(length-1) do |i|
            left,right = p.split_at(i)
            yield left.append(right.cons(head))
          end
        end
      end
      return self
    end
def permutation(length = size, &block)
      raise Erlang::ImproperListError if improper?
      return enum_for(:permutation, length) if not block_given?
      if length == 0
        yield Erlang::Nil
      elsif length == 1
        each { |obj| yield Erlang::Cons.new(obj, Erlang::Nil) }
      elsif not empty?
        if length < size
          tail.permutation(length, &block)
        end

        tail.permutation(length-1) do |p|
          0.upto(length-1) do |i|
            left,right = p.split_at(i)
            yield left.append(right.cons(head))
          end
        end
      end
      return self
    end
Yields all permutations of length `n` of the items in the list, and then
 returns `self`. If no length `n` is specified, permutations of the entire
 list will be yielded.

 There is no guarantee about which order the permutations will be yielded in.

 If no block is given, an `Enumerator` is returned instead.

 @example
   Erlang::List[1, 2, 3].permutation.to_a
   # => [Erlang::List[1, 2, 3],
   #     Erlang::List[2, 1, 3],
   #     Erlang::List[2, 3, 1],
   #     Erlang::List[1, 3, 2],
   #     Erlang::List[3, 1, 2],
   #     Erlang::List[3, 2, 1]]

 @return [self, Enumerator]
 @yield [list] Once for each permutation.