Re: Next: eval in procedure-environment

From: Keith Wright <kwright_at_tiac.net>
Date: Sun, 16 Mar 97 17:33 EST

>
> If I create an expression in the proposed way:
>
> (define b2 '(lambda () (lambda (x) (+ x 1))))
>
> Now I don't see a way to give it the requested parameter, using
> eval... Damn!
>

Why do you want to use eval? Are you trying to understand eval, or
are you trying to get a job done? If the former, have fun. If the
later, eval probably has nothing to do with it.

> Any suggestions? Is there another way to create objects that
> have their own data and to which one can add (and maybe delete)
> functions after creation, which access the data?

Just do what you said---and you never said eval:

(define thing
  (let ((thing-state 0)
        (symbol-function-list '()))
     (lambda (command . args)
        (case command
           ((deffun) (set! symbol-function-list
                           (cons (cons (car args) (cadr args))
                                 symbol-function-list))
                     'done)
;;; ((delfun) (set! symbol-function-list ... etc))
           ;; other built-in functions here
           (else
             (let ((sym-fun (assq command symbol-function-list)))
               (if sym-fun
                   (let ((result (apply (cdr sym-fun) thing-state args)))
                      (set! thing-state (cdr result))
                      (car result))
                   ;; not on list so ``delegate''
                   (apply thing2 command args))))))))

;;; Now 'deffun is command to add function definitions, which
;;; consist of a symbol followed by a function. The first argument
;;; of the function is the state, other arguments may follow.
;;; The function must return a pair consisting of a value for the
;;; call and a new state.

(thing 'deffun 'add (lambda(state . args)
                       (set! state (+ (car args) state))
                       (cons state state)))
(thing 'add 4)
==> 4
(thing 'add 5)
==> 9
(thing 'deffun 'clear (lambda(state . args)
                         (set! state 0)
                         (cons "All clear" state)))
(thing 'clear)
==> "All clear"
(thing 'add 3)
==> 3

This is adding numbers the hard way, but maybe you have an application
for this sort of thing. But "why?" is a good question.

>The word "meta object protocol" comes to my mind... (????)

There's no word like that in my mind. A hyphen could make it into two
words, but I don't know what they mean.

> Best would be something portable.
 
Eval is not in R4RS. Many implementations have added a function
with this name, but they differ in what it does. But all Scheme
implementations have symbols, functions, lists, local variables,
and apply.

-- 
     --Keith
This mail message sent by GNU emacs and Linux.
Power to the people. Linux is here.
Food, Shelter, Source code.
Received on Sun Mar 16 1997 - 23:34:42 CET

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST