```正木です。２、３回に分けて送ります。

Sequence.rb
----------
include Math

class Sequence
def initialize(list=[],*x)
@list=list
@x=x
@proc=eval("proc{"+yield+"}")
end
def [](n)
@list[n] || @list[n]=@proc.call(n,*@x)
end
def []=(n,a)
@list[n]=a
end
def each(n=0,m=nil)
if not m
i=n
loop do
yield(self[i])
i+=1
end
else
(n..m).each{|i| yield(self[i])}
end
end

def each_index(n=0,m=nil)
if not m
i=n
loop do
yield(i)
i+=1
end
else
(n..m).each{|i| yield(i)}
end
end

def each_with_index(n=0,m=nil)
if not m
i=n
loop do
yield(self[i],i)
i+=1
end
else
(n..m).each{|i| yield(self[i],i)}
end
end

def succ_each(n=0,m=nil)
each_index{|i| yield(self[i],self[i+1],i)}
end

def list(n=0,m=nil)
if m
(n..m).collect{|i| self[i]}
else
@list
end
end

def find(n=0,m=nil)
if m
list(n,m).find{|k| yield k}
else
@list.find{|k| yield k} ||
(r=nil;each{|x| if yield(x) then r=x;break;end};r)
end
end

def find_with_index
r=nil;each_with_index{|x,i| if yield(x,i) then r=i;break;end};r
end

def succ_find
r=nil;succ_each{|x,y,i| if yield(x,y,i) then r=x;break;end};r
end

def sum(n=0,m=nil,s=nil)
each(n,m){|x| (break if not yield(x))  if iterator?;if s;s+=x;else s=x end
}
s
end

def sum_i(n=0,m=nil,s=nil)
each_with_index(n,m){|x,i| (break if not yield(x,i))  if iterator?;if s;s+
=x;else s=x end}
s
end

def Sum
x=self
Sequence.new([x[0]],x){"|n,x| self[n-1]+x[n]"}
end

def Diff
x=self
Sequence.new([x[0]],x){"|n,x| x[n]-x[n-1]"}
end

def Prod
x=self
Sequence.new([x[0]],x){"|n,x| self[n-1]*x[n]"}
end

def prod(n=0,m=nil,s=1)
each_with_index(n,m){|x,i| (break if not yield(x,i))  if iterator?;s*=x}
s
end

def shift(m=1)
x=self
Sequence.new([],x,m){"|n,x,m| x[n+m]"}
end

def	+(y)
x=self
Sequence.new([],x,y){"|n,x,y| x[n]+y[n]"}
end

def	-(y)
x=self
Sequence.new([],x,y){"|n,x,y| x[n]-y[n]"}
end

def	-@
x=self
Sequence.new([],x){"|n,x| -x[n]"}
end

def	*(y)
x=self
case y
when Numeric
Sequence.new([],x,y){"|n,x,y| x[n]*y"}
else
Sequence.new([],x,y){"|n,x,y| x[n]*y[n]"}
end
end

def	/(y)
x=self
case y
when Numeric
Sequence.new([],x,y){"|n,x,y| x[n]/y"}
else
Sequence.new([],x,y){"|n,x,y| x[n]/y[n]"}
end
end

def inverse
x=self
Sequence.new([],x){"|n,x| 1/x[n]"}
end

def **(m)
x=self
Sequence.new([],x,m){"|n,x,m| x[n]**m "}
end

def	&(y)    # convolution
x=self
Sequence.new([],x,y){"|n,x,y| (0..n).collect{|i| x[i]*y[n-i]}.sum"}
end
end
----------
Sequence.sample.rb
-----------------
Nat=Sequence.new([]){"|n| n"}
Sgn2=Sequence.new([1,-1]){"|n| self[n-2]"}
Fact=Sequence.new([1]){"|n| n*self[n-1]"}
Binom =Sequence.new([Sequence.new([1]){"|i| 0"}]){"|n|
Sequence.new([1],n){\"|i,n| Binom[n-1][i-1]+Binom[n-1][i]\"}"}
Fibonacci=Sequence.new([0,1]){"|n| self[n-2]+self[n-1]"}

def (Power={}).[](x)
super || Power[x]=Sequence.new([1],x){"|n,x| x*self[n-1]"}
end

def (ExpS={}).[](x)
super || ExpS[x]=Power[x]/Fact
end
def (Exp={}).[](x)
super || Exp[x]=ExpS[x].Sum
end

require "rational.rb"
Bernoulli =Sequence.new([1,Rational(-1,2)]){"|n| -(Binom[n+1]*Bernoulli).sum(0
,n-1)/(n+1)"}

class SequenceHash
def initialize(list)
@hash={}
@list=list
@proc=proc
end
def [](s)
@hash[s] || @hash[s]=Sequence.new(@list,s,&@proc)
end
end

#Power=SequenceHash.new([1]){"|n,x| x*self[n-1]"}
---------

```