1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
# -*- coding: utf-8 -*-
require_relative 'list'
module MIKU
class Cons
include List
include Enumerable
attr_reader(:car, :cdr)
def self.list(*nodes)
unless nodes.empty?
carnode, *cdrnode = *nodes
Cons.new(carnode, list(*cdrnode))
end
end
def initialize(car, cdr=nil)
@car = car
@cdr = cdr
end
def setcar(val)
@car = val
self
end
def setcdr(val)
@cdr = val
self
end
def each(&proc)
proc.call @car
@cdr.each(&proc) if(@cdr.is_a? Enumerable)
end
def to_cons
self
end
def empty?
false
end
def inspect
"(#{car.inspect}"+(if @cdr.is_a?(Cons) then " #{@cdr.inspect[1..-1]}"
elsif @cdr === nil then ')'
else " . #{@cdr.inspect})" end)
end
def deconstruct
to_a
end
end
end
|