class Rus3::Pair

A building block to construct a dotted pair.

Attributes

car[R]

CAR part of the pair.

cdr[R]

CDR part of the pair.

Public Class Methods

new(car, cdr) → a new Pair object click to toggle source
# File lib/rus3/pair.rb, line 19
def initialize(car = EMPTY_LIST, cdr = EMPTY_LIST)
  @car = car
  @cdr = cdr
end

Public Instance Methods

==(other) click to toggle source

Compares to an other pair.

# File lib/rus3/pair.rb, line 38
def ==(other)
  other.instance_of?(Pair) and @car == other.car and @cdr == other.cdr
end
set_car!(obj) click to toggle source

Replaces the CAR part with the argument.

# File lib/rus3/pair.rb, line 26
def set_car!(obj)
  @car = obj
end
set_cdr!(obj) click to toggle source

Replaces the CDR part with the argument.

# File lib/rus3/pair.rb, line 32
def set_cdr!(obj)
  @cdr = obj
end
to_a() click to toggle source

Converts to an Array, which looks like as follows:

[CAR, CDR]

When CAR or CDR part is also a Pair object, converts recursively.

# File lib/rus3/pair.rb, line 49
def to_a
  [@car, @cdr].map { |e| Pair === e ? e.to_a : e}
end
to_s() click to toggle source

Converts to a String. Normally, uses the dot-pair notaion which looks like “(CAR . CDR)”. If the CDR part is an empty string, looks like a normal list which has a single element, like “(CAR)”.

# File lib/rus3/pair.rb, line 58
def to_s
  car_part = null?(@car) ? "()" : @car
  if null?(@cdr)
    "(#{car_part})"
  else
    "(#{car_part} . #{@cdr})"
  end
end