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.



144
145
146
147
# File 'lib/ruby-prolog/ruby-prolog.rb', line 144

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

Instance Method Details

#inspectObject



149
150
151
152
153
154
155
156
157
158
# File 'lib/ruby-prolog/ruby-prolog.rb', line 149

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



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruby-prolog/ruby-prolog.rb', line 160

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