Why doesn't Scheme support first class environments?

后端 未结 4 1704
慢半拍i
慢半拍i 2021-01-31 20:04

I\'ve been reading through SICP (Structure and Interpration of Computer Programs) and was really excited to discover this wonderful special form: \"make-environment\", which the

4条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 20:24

    After more digging around I discovered this informative thread on newsnet:

    "The R5RS EVAL and environment specifiers are a compromise between those who profoundly dislike first-class environments and want a restricted EVAL, and those who can not accept/understand EVAL without a second argument that is an environment."

    Also, found this "work-around":

    (define-syntax make-environment 
      (syntax-rules () 
        ((_ definition ...) 
         (let ((environment (scheme-report-environment 5))) 
           (eval '(begin definition 
                         ...) 
                 environment) 
           environment)))) 
    
    
    (define arctic 
      (make-environment 
        (define animal 'polarbaer))) 
    

    (taken from this)

    However, I ended up adopting a "message passing" style kinda of like the first guy suggested - I return an alist of functions, and have a generic "send" method for invoking a particular function by name... i.e something like this

    (define multiply
      (list
        (cons 'differentiate (...))
        (cons 'evaluate (lambda (args) (apply * args)))))
    
    (define lookup
      (lambda (name dict)
        (cdr (assoc name dict))))
    
    ; Lookup the method on the object and invoke it
    (define send
      (lambda (method arg args)
        ((lookup method arg) args))
    
    ((send 'evaluate multiply) args)
    

    I've been reading further and am aware that there's all of CLOS if I really wanted to adopt a fully OO style - but I think even above is somewhat overkill.

提交回复
热议问题