I cannot seem to figure out a way to do Scheme's cons boxes in Ruby (it seems its pretty much all arrays). This is a pretty rough outline:
class cons
def initialize (car, cdr)
@car = car
@cdr = cdr
end
#return the car of the pair
def car
return @car
end
#return the cdr of the pair
def cdr
return @cdr
end
end
I can pass two values and call the car
and cdr
, but this is not a list of any sort (just two values). How do I make a list on which I can insert something as in Scheme cons:
myCons = (cons(1, cons(2, cons(3, cons(4, 5)))))
The closest I can find is making my own array like myArray = Array[1, 2, 3, 4, 5]
and then using puts myArray.join(' ')
. This only gives me "1 2 3 4 5"
and not (1 2 3 4 5)
though, and that's not taking into account I still can't actually build the array with cons, I simply made it myself.