Variable references in lisp

前端 未结 7 863
灰色年华
灰色年华 2020-11-29 07:04

Another newbie (Common) LISP question:

Basically in most programming languages there\'s a mean for functions to receive references to variables instead of just value

7条回答
  •  独厮守ぢ
    2020-11-29 07:44

    As a beginner I came here to figure out how to do what should be a trivial procedure in any language. Most of the solutions posted above did not work properly, may have been more complicated than what was needed, or different implementations. Here is a simple solution for SBCL:

    (defmacro inc-by-num (var num)
               (set var (+ (eval var) num)))
    

    Apparently you cannot use setf b/c it restricts the scope whereas set doesn't. Also you may need to use eval before var if you get an "argument X is not a number" error.

提交回复
热议问题