News for hackers. Stuff that matters.
Re:Lisp in about 200 lines of Ruby by mahmud on Monday November 09, 2009 @09:26AM (#930928)
Under which lisp implementation do those forms work?

The definitions for CAR and CDR are identical and they don't make much sense (set the symbol 'car to the value of a function that takes angument xs, which is a function, and applies that to the symbol 'car)

And the definition for CONS is equally opaque.

How would you use those functions, CONS, CAR and CDR?

Since you pretty much nailed the uses of church numerals, monads and the y-combinator, I know you know what you're talking about, it just that Lisp's semantics is confusing me (it has common lisp keyword SETQ but none of its behavior)

Re:Lisp in about 200 lines of Ruby by twilightsentry on Monday November 09, 2009 @02:14PM (#931530)
Oops; I meant to write those in CL, but I forgot about functions having their own namespace (been playing too long in a lisp-1). It should work in CL if you use FUNCALL.

If mquander's explanation isn't enough, you might look at it using terms from the OO world: CAR takes a pair object and calls its 'car method.

Re:Lisp in about 200 lines of Ruby by mahmud on Monday November 09, 2009 @10:42PM (#932396)
So you mean a meta-circular OOP implementation, where the hosting language already has car, cdr and cons. Alright.

I thought what you wrote was supposed to be a primitive implementation of the functions.

Re:Lisp in about 200 lines of Ruby by twilightsentry on Saturday November 14, 2009 @07:32PM (#942397)
No, not really, unless you're running the code in a metacircular interpreter.

Imagine encoding the pair ('hello . 'world) into a function. You might say:

  (lambda (msg)
    (if (eq msg 'car) 'hello 'world))
You could get 'hello by applying that function to the 'car symbol, or 'world by applying it to anything else. Wrapping that in another function lets us generalise to any CAR/CDR.

  (lambda (car-value cdr-value)
    (lambda (msg)
      (if (eq msg 'car) car-value cdr-value)))
Re:Lisp in about 200 lines of Ruby by mquander on Monday November 09, 2009 @10:05AM (#930990)
Those look correct to me. The function returned by cons is just a switch that holds two values in a closure, and interprets what you pass it as a signal to determine which to retrieve. You could just as easily rewrite it to pass it 0 or 1 instead of car and cdr.

If you evaluate (car (cons 1 2)) and (cdr (cons 1 2)) on paper using substitution, you'll see how it works.

Re:Lisp in about 200 lines of Ruby by dkersten on Monday November 09, 2009 @11:46AM (#931217)
Exactly. SICP shows some similar code, though I don't remember which part..