0

Can anyone briefly explain to me how message passing is implemented in scheme? I think I am little off on the whole concept of message passing.

Adeq Hero
  • 137
  • 1
  • 3
  • 11
  • 3
    This is a very vague question so it is hard to answer. Can you try to ask a more specific/detailed question? Do you mean in the object-oriented sense? If so, you can look at Dan Friedman's ["Object-Oriented Style"](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.69.7199&rep=rep1&type=pdf). Or do you mean in the concurrency sense? (there is a [section](http://library.readscheme.org/page9.html) on concurrency at ReadScheme.org you can look at) – Asumu Takikawa Aug 01 '12 at 23:17

2 Answers2

2

Take a look at SICP.

http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-17.html#%_sec_2.4.1

http://www.michaelharrison.ws/weblog/?p=50

Sam Tobin-Hochstadt
  • 4,983
  • 1
  • 21
  • 43
soegaard
  • 30,661
  • 4
  • 57
  • 106
0

Message passing in the context of closures

The following example defines a closure implementing a simple calculator. The function make-calculator is similar to what object oriented languages call the constructor. The difference is: make-calculator returns a function, while constructors return object values. In object oriented languages object values are first class values. Scheme does not have such values. An object first class values provides the functionality to access object member variables and object methods. In Scheme this functionality has to be emulated by the definition of a dispatch function. make-calculator returns such a function. The body of make-calculator defines

  • two variables a and b (member variables)
  • two mutation functions set-a! and set-b! (accessors)
  • four evaluation functions addition, subtraction, multiplication and division (methods)

The above definitions are local to the closure make-calculator. In an object oriented language they are called private. The dispatch function makes the functions public and keeps the variables private. This works, because the dispatch function has access to the local scope of the make-calculator closure. The dispatch function accepts a message and returns the matching function. This exposes the local functions to the caller of the dispatch function.

(define (make-calculator)
  
  (define a)
  (define b)

  (define (set-a! value)
    (set! a value))

  (define (set-b! value)
    (set! b value))

  (define (addition)
    (+ a b))

  (define (subtraction)
    (- a b))

  (define (multiplication)
    (* a b))

  (define (division)
    (/ a b))

  (lambda (message)
    (case message
      ((set-a!) set-a!)
      ((set-b!) set-b!)
      ((addition) addition)
      ((subtraction) subtraction)
      ((multiplication) multiplication)
      ((division) division))))

First the constructor has to be called to create an "object". calc is the dispatch function, which accepts different messages, which are just symbols.

(define calc (make-calculator))

Sending a message means calling the dispatch function with a symbol argument. The following sends the message set-a! to calc, which returns the value of the local function set-a!. The name of the message and the name of the local function are the same in this case. This helps to avoid confusion, but it is not required.

(calc 'set-a!)     ;; => #<procedure set-a!>

Because calc returns a function, an additional application is necessary to call the accessor. The following sets a to 3 and b to 5.

((calc 'set-a!) 3)
((calc 'set-b!) 5)

Now we can calculate:

((calc 'addition))        ;; => 8
((calc 'subtraction))     ;; => -2
((calc 'multiplication))  ;; => 15
((calc 'division))        ;; => 3/15

The code works this way in Chez Scheme.

ceving
  • 21,900
  • 13
  • 104
  • 178