setq vs Undeclared free variable (common lisp)

后端 未结 3 1033
不思量自难忘°
不思量自难忘° 2021-01-18 17:32

I\'m a total Lisp n00b, so please be gentle.

I\'m having trouble wrapping my head around CL\'s idea of an [un-]declared free variable. I would think that:



        
3条回答
  •  感动是毒
    2021-01-18 18:10

    The Common Lisp HyperSpec (basically its the Common Lisp standard in HTML form) says:

    http://www.lispworks.com/documentation/HyperSpec/Body/s_setq.htm

    Assigns values to variables.

    So SETQ only assigns values to variables. It does not declare them.

    Variable definitions are done globally with DEFVAR, DEFPARAMETER, ...

    (defparameter *this-is-a-global-dynamic-variable* 'yep)
    

    Variable definitions are done locally with DEFUN, LET, LET*, LOOP, and many others.

    (defun foo (v1 v2)
      ...)
    
    (let ((v1 10)
          (v2 20))
      ...)
    
    (loop for v1 in '(10 30 10 20)
          do ...)
    

    This is basic Lisp and it would be useful to read an introduction. I would recommend:

    http://www.cs.cmu.edu/~dst/LispBook/

    Above book is free for download.

    Additionally the above mentioned Common Lisp Hyperspec provides you with the definitions for Common Lisp and describes the various facilities (DEFUN, LOOP, DEFPARAMETER, ...) in detail.

提交回复
热议问题