How is set! defined in scheme?

后端 未结 6 1143
囚心锁ツ
囚心锁ツ 2020-12-20 05:45

How would you implement your own set! function in Scheme? A set! function is a destructive procedure that changes a value that is defined taking into account th

6条回答
  •  有刺的猬
    2020-12-20 06:21

    In scheme there are 2 "global" environments.

    There is an environment where you store your highest defined symbols and there is another environment where are stored the primitive functions and the global variables that represent parameters of the system.

    When you write something like (set! VAR VAL), the interpreter will look for the binding of VAR.

    You cannot use set! on a variable that was not bound. The binding is done either by define or by lambda or by the system for the primitive operators.

    Binding means allocating a location in some environment. So the binding function has the signature symbol -> address.

    Coming back to set!, the interpreter will look in the environment where VAR is bound. First it looks in local environment (created by lambda), then it its local parent environment, and so on, then in global environment, then in system environment (in that order) until it finds an environment frame that contains a binding for VAR.

提交回复
热议问题