Class: RubyProlog::Cons

Inherits:
Array
  • Object
show all
Defined in:
lib/ruby-prolog/ruby-prolog.rb

Overview

Lisp

Instance Method Summary collapse

Constructor Details

#initialize(car, cdr) ⇒ Cons

Returns a new instance of Cons.



152
153
154
155
# File 'lib/ruby-prolog/ruby-prolog.rb', line 152

def initialize(car, cdr)
  super(2)
  self[0], self[1] = car, cdr
end

Instance Method Details

#inspectObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/ruby-prolog/ruby-prolog.rb', line 157

def inspect
  repr = proc {|x|
    car, cdr = x[0], x[1]
    if cdr.nil? then [car.inspect]
    elsif Cons === cdr then repr[cdr].unshift(car.inspect)
    else [car.inspect, '.', cdr.inspect]
    end
  }
  return '(' + repr[self].join(' ') + ')'
end

#to_prologObject



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby-prolog/ruby-prolog.rb', line 168

def to_prolog
  current = self
  array = []
  while current
    array << case current[0]
      when :CUT then '!'
      when :_ then '_'
      else current[0].to_prolog
      end
    current = current[1]
  end
  return array.join(', ')
end