What's difference between defvar, defparameter, setf and setq

前端 未结 2 954
陌清茗
陌清茗 2020-12-04 10:04

I found a Similar question.

But I don\'t quite understand that explanation.

So I\'m trying to run clisp with the following example:

  [1]>         


        
2条回答
  •  粉色の甜心
    2020-12-04 10:37

    Both defvar and defparameter will declare a variable as a "dynamically scoped variable". In addition, defparameter will always set the value of the variable to the value you pass in as the second argument. This is different from defvar, it will only set the value of the variable if it previously hasn't been set.

    Defining a variable with setf or setq in the global lexical scope is undefined. Some implementations will create a dynamically scoped variable for you, some will not. You may see diagnostic messages when you do it for the first time.

    To understand the difference between lexically-scoped and dynamically-scoped variables, try the following code snippet:

    * (defvar *a* 1)
    
    *A*
    * (let ((*a* 5)) (defun demo-a () *a*))
    
    DEMO-A
    * (let ((b 5)) (defun demo-b () b))
    
    DEMO-B
    * (let ((*a* 100)) (demo-a))
    
    100
    * (let ((b 100)) (demo-b))
    
    5
    

    Here we create a dynamically-scoped variable and a function that return the value (defined inside a binding where it has a different value during the function creation, this is not necessary and done only to look similar to the lexical closure over b). We then define a new variable and define a function return its value.

    After that, we call both functions, inside closures binding a value to a variable of the same name. In the dynamic scoping case, it is the same variable. In the lexical closure case (b), they merely have the same name, but are not the same variable, since they're defined in two different lexical closures.

    As far as the difference between setf and setq, try to always use setf (I cannot think of any example where (setq blah blahblah) would work and (setf blah blahblah) wouldn't do the same thing).

提交回复
热议问题