この広告は、90日以上更新していないブログに表示しています。
今回の主な修正点は…
class NilClass def to_s "_" endendCall = lambda {|f, *a| lambda { f[f, *a] } }Walk = lambda {|n, xs, eachf, lastf| Call[ lambda {|fs, xs, n, eachf, lastf| if n > 0 y, ys = xs[] eachf[y, ys] fs[fs, ys, n - 1, eachf, lastf] else lastf[xs] end }, xs, n, eachf, lastf ]}Show = lambda {|xs| Walk[10, xs, lambda {|y, ys| print "#{y}, "}, lambda {|ys| puts "..." } ][]}Drop = lambda {|xs, n| Walk[n, xs, lambda {|y, ys|}, lambda {|ys| ys} ][]}Zip = lambda {|op, s, t| x, xs = s[] y, ys = t[] lambda { return op[x, y], Zip[op, xs, ys] }}Sequence = lambda {|s, *a| lambda { return a[0], s[s, *a] }}Add = lambda {|x, y| x + y }Mul = lambda {|x, y| x * y }Power = lambda {|x, y| x ** y }OfConstant = lambda {|s, a| Sequence[s, a]}OfNatural = lambda {|s, a| Sequence[s, a + 1]}OfFibonacci = lambda {|s, a, b| Sequence[s, b, a + b]}OfGiven = lambda {|s, *a| Sequence[s, *a[1..-1]]}DoubleOf = lambda {|s| Zip[Add, s, s]}ShiftOf = lambda {|s| Drop[s, 1]}PowerOf = lambda {|s| Zip[Power, s, Natural]}One = Sequence[OfConstant, 1]Two = Sequence[OfConstant, 2]Ten = Sequence[OfConstant, 10]Natural = Sequence[OfNatural, 0]NaturalPlus = ShiftOf[Natural]Fibonacci = Sequence[OfFibonacci, 0, 1]Even = DoubleOf[Natural]Odd = Zip[Add, Even, One]Sample = Sequence[OfGiven, 3, 1, 4, 1, 5, 9]PowerOfTwo = PowerOf[Two]PowerOfTen = PowerOf[Ten]Show[One]Show[Natural]Show[Fibonacci]Show[NaturalPlus]Show[Even]Show[Odd]Show[Sample]Show[PowerOfTwo]Show[PowerOfTen]実行結果です。
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...0, 2, 4, 6, 8, 10, 12, 14, 16, 18, ...1, 3, 5, 7, 9, 11, 13, 15, 17, 19, ...3, 1, 4, 1, 5, 9, _, _, _, _, ...1, 2, 4, 8, 16, 32, 64, 128, 256, 512, ...1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, ...
引用をストックしました
引用するにはまずログインしてください
引用をストックできませんでした。再度お試しください
限定公開記事のため引用できません。