Scheme: Procedures that return another inner procedure
stackoverflow.com
stackoverflow.com
The outer function (cons) is a class constructor. It returns an object, which is a function of one argument, where the argument is equivalent to the name of a method. In this case, the methods are getters, so they evaluate to values. You could just as easily have stored more procedures in the object returned by the constructor.
In this case, numbers where chosen as method names and sugary procedures defined outside the object itself. You could have used symbols:
(define (cons x y)
(lambda (method)
(cond ((eq? method 'car) x)
((eq? method 'cdr) y)
(else (error "unknown method")))))
In which case what you have more closely resembles OO: # (define p (cons 1 2))
# (p 'car)
1
# (p 'cdr)
2 (defn pcons [x y] (fn [m] (m x y)))
(defn pcar [z] (z (fn [p q] p)))
(defn pcdr [z] (z (fn [p q] q)))
(pcar (pcons 2 3)) ==> 2
(pcdr (pcons 2 3)) ==> 3(The "above the line" document mentioned is here: http://www-inst.eecs.berkeley.edu/~cs61a/reader/aboveline.pd...)
Not sure how much overlap the two have.
You can compile OO code into assembler. You can implement it with functions and closures. You can run it on a VM with special support for virtual dispatch.
In order to be an effective abstraction, you have to think of it as being different. I’d go so far as to say, if you look at OO and say to yourself, “this is just inside-out objects” and dismiss it as not brining anything to the table, you’re missing the point entirely.
Of course, you ought to realize that an object is an inside-out closure, and a closure is an inside-out object. But as an enlightened thinker, you can hold that idea in your head and simultaneously hold in your head the idea that they are different things.
What I was getting at is that Object Oriented Programming Languages are typically presented in "popular culture" as something discrete and concrete: that C++ is as discontinuous with Scheme as walruses are with penguins. And by this I'm not making a beeline for the Turning tarpit because we are talking about the same abstraction: dispatch.
I suppose that's why I haven't ever found object oriented programming fit quite right into the wrinkles in my cerebrum. I'm still trying force fitting the world into a model based on the computational idiom of dispatch...it's selecting an implementation mechanism as the first step. Object oriented programming feels to me like it is primarily an abstraction about computers not the world...and the simpler abstraction of dispatch works better for me because it is simply simpler.
Funny thinking that Lisp started as a blackboard experiment.
[1] unless you hit P vs NP territory.