Sha256: 253c9101cfa6eb2d27a286b8fd3e5a326ecf7c4d7815f4a41ad844f6bf0a9a91

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require_relative 'composite_term'

unless MiniKraken::Core.constants(false).include? :ConsCell
  module MiniKraken
    module Core
      class ConsCell < CompositeTerm
        attr_reader :car
        attr_reader :cdr

        def initialize(obj1, obj2 = nil)
          @car = obj1
          @cdr = obj2
        end

        def children
          [car, cdr]
        end

        # Return true if it is an empty list, otherwise false.
        # A list is empty, when both car and cdr fields are nil.
        def null?
          car.nil? && cdr.nil?
        end

        def ==(other)
          return false unless other.respond_to?(:car)

          (car == other.car) && (cdr == other.cdr)
        end

        def eql?(other)
          (self.class == other.class) && car.eql?(other.car) && cdr.eql?(other.cdr)
        end

        def quote(anEnv)
          return self if null?

          new_car = car.nil? ? nil : car.quote(anEnv)
          new_cdr = cdr.nil? ? nil : cdr.quote(anEnv)
          ConsCell.new(new_car, new_cdr)
        end

        def append(another)
          @cdr = another
        end
      end # class

      # Constant representing the null (empty) list.
      NullList = ConsCell.new(nil, nil).freeze
    end # module
  end # module
end # defined

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mini_kraken-0.1.13 lib/mini_kraken/core/cons_cell.rb
mini_kraken-0.1.12 lib/mini_kraken/core/cons_cell.rb
mini_kraken-0.1.11 lib/mini_kraken/core/cons_cell.rb
mini_kraken-0.1.10 lib/mini_kraken/core/cons_cell.rb
mini_kraken-0.1.09 lib/mini_kraken/core/cons_cell.rb
mini_kraken-0.1.08 lib/mini_kraken/core/cons_cell.rb
mini_kraken-0.1.07 lib/mini_kraken/core/cons_cell.rb