LISP用字符串连接变量名

LISP用字符串连接变量名,lisp,Lisp,我想知道我是否可以在Lisp中执行类似的操作: 我需要声明n个变量。所以它们是n1,n2,n3…等等 (dotimes (i n) (setq (+ 'n i)) 有可能吗?雷纳·乔斯维格在一篇评论中指出,你所得到的代码不适用于你正在尝试做的事情,并解释了原因。如果您试图以编程方式声明变量,则需要对源代码进行操作,这意味着您需要一个宏。在这种情况下,这很容易。我们可以定义一个带有索引变量的宏,该宏接受一个符号、一个数字和一个主体,并使用您期望的变量扩展为一个let,并在该范围内计算主体: (d

我想知道我是否可以在Lisp中执行类似的操作:

我需要声明n个变量。所以它们是n1,n2,n3…等等

(dotimes (i n) (setq (+ 'n i))

有可能吗?

雷纳·乔斯维格在一篇评论中指出,你所得到的代码不适用于你正在尝试做的事情,并解释了原因。如果您试图以编程方式声明变量,则需要对源代码进行操作,这意味着您需要一个宏。在这种情况下,这很容易。我们可以定义一个带有索引变量的宏,该宏接受一个符号、一个数字和一个主体,并使用您期望的变量扩展为一个let,并在该范围内计算主体:

(defmacro with-indexed-vars ((var n) &body body)
  "Evalutes BODY within a lexical environment that has X1...XN
declared as variables by LET.  For instance

    (with-indexed-vars (x 5)
      (list x1 x2 x3 x4 x5))

expands to 

    (LET (X1 X2 X3 X4 X5)
      (LIST X1 X2 X3 X4 X5))

The symbols naming the variables declared by the LET are interned
into the same package as VAR.  All the variables are initialized
to NIL by LET."
  (let ((name (symbol-name var)))
    `(let ,(loop for i from 1 to n
              collecting (intern (concatenate 'string name (write-to-string i))
                                 (symbol-package var)))
       ,@body)))
然后,我们可以这样使用它:

(with-indexed-vars (n 4)
  (setq n3 "three")
  (setq n4 4)
  (list n4 n1 n3 n2))

;=> (4 NIL "three" NIL)
正如Sean Allred在一篇评论中所指出的,这是开始Lisp编程的一个高级主题。如果您知道需要n个值单元格,那么您最好使用向量和aref来访问这些值:

(let ((ns (make-array 4 :initial-element nil)))
  (setf (aref ns 2) "three")
  (setf (aref ns 3) 4)
  ns)

;=> #(NIL NIL "three" 4)

Rainer Joswig在一篇评论中指出,您的代码不适用于您正在尝试的工作,并解释了原因。如果您试图以编程方式声明变量,则需要对源代码进行操作,这意味着您需要一个宏。在这种情况下,这很容易。我们可以定义一个带有索引变量的宏,该宏接受一个符号、一个数字和一个主体,并使用您期望的变量扩展为一个let,并在该范围内计算主体:

(defmacro with-indexed-vars ((var n) &body body)
  "Evalutes BODY within a lexical environment that has X1...XN
declared as variables by LET.  For instance

    (with-indexed-vars (x 5)
      (list x1 x2 x3 x4 x5))

expands to 

    (LET (X1 X2 X3 X4 X5)
      (LIST X1 X2 X3 X4 X5))

The symbols naming the variables declared by the LET are interned
into the same package as VAR.  All the variables are initialized
to NIL by LET."
  (let ((name (symbol-name var)))
    `(let ,(loop for i from 1 to n
              collecting (intern (concatenate 'string name (write-to-string i))
                                 (symbol-package var)))
       ,@body)))
然后,我们可以这样使用它:

(with-indexed-vars (n 4)
  (setq n3 "three")
  (setq n4 4)
  (list n4 n1 n3 n2))

;=> (4 NIL "three" NIL)
正如Sean Allred在一篇评论中所指出的,这是开始Lisp编程的一个高级主题。如果您知道需要n个值单元格,那么您最好使用向量和aref来访问这些值:

(let ((ns (make-array 4 :initial-element nil)))
  (setf (aref ns 2) "three")
  (setf (aref ns 3) 4)
  ns)

;=> #(NIL NIL "three" 4)

Rainer Joswig在一篇评论中指出,您的代码不适用于您正在尝试的工作,并解释了原因。如果您试图以编程方式声明变量,则需要对源代码进行操作,这意味着您需要一个宏。在这种情况下,这很容易。我们可以定义一个带有索引变量的宏,该宏接受一个符号、一个数字和一个主体,并使用您期望的变量扩展为一个let,并在该范围内计算主体:

(defmacro with-indexed-vars ((var n) &body body)
  "Evalutes BODY within a lexical environment that has X1...XN
declared as variables by LET.  For instance

    (with-indexed-vars (x 5)
      (list x1 x2 x3 x4 x5))

expands to 

    (LET (X1 X2 X3 X4 X5)
      (LIST X1 X2 X3 X4 X5))

The symbols naming the variables declared by the LET are interned
into the same package as VAR.  All the variables are initialized
to NIL by LET."
  (let ((name (symbol-name var)))
    `(let ,(loop for i from 1 to n
              collecting (intern (concatenate 'string name (write-to-string i))
                                 (symbol-package var)))
       ,@body)))
然后,我们可以这样使用它:

(with-indexed-vars (n 4)
  (setq n3 "three")
  (setq n4 4)
  (list n4 n1 n3 n2))

;=> (4 NIL "three" NIL)
正如Sean Allred在一篇评论中所指出的,这是开始Lisp编程的一个高级主题。如果您知道需要n个值单元格,那么您最好使用向量和aref来访问这些值:

(let ((ns (make-array 4 :initial-element nil)))
  (setf (aref ns 2) "three")
  (setf (aref ns 3) 4)
  ns)

;=> #(NIL NIL "three" 4)

Rainer Joswig在一篇评论中指出,您的代码不适用于您正在尝试的工作,并解释了原因。如果您试图以编程方式声明变量,则需要对源代码进行操作,这意味着您需要一个宏。在这种情况下,这很容易。我们可以定义一个带有索引变量的宏,该宏接受一个符号、一个数字和一个主体,并使用您期望的变量扩展为一个let,并在该范围内计算主体:

(defmacro with-indexed-vars ((var n) &body body)
  "Evalutes BODY within a lexical environment that has X1...XN
declared as variables by LET.  For instance

    (with-indexed-vars (x 5)
      (list x1 x2 x3 x4 x5))

expands to 

    (LET (X1 X2 X3 X4 X5)
      (LIST X1 X2 X3 X4 X5))

The symbols naming the variables declared by the LET are interned
into the same package as VAR.  All the variables are initialized
to NIL by LET."
  (let ((name (symbol-name var)))
    `(let ,(loop for i from 1 to n
              collecting (intern (concatenate 'string name (write-to-string i))
                                 (symbol-package var)))
       ,@body)))
然后,我们可以这样使用它:

(with-indexed-vars (n 4)
  (setq n3 "three")
  (setq n4 4)
  (list n4 n1 n3 n2))

;=> (4 NIL "three" NIL)
正如Sean Allred在一篇评论中所指出的,这是开始Lisp编程的一个高级主题。如果您知道需要n个值单元格,那么您最好使用向量和aref来访问这些值:

(let ((ns (make-array 4 :initial-element nil)))
  (setf (aref ns 2) "three")
  (setf (aref ns 3) 4)
  ns)

;=> #(NIL NIL "three" 4)

四个错误:
(setq(+'ni))
:1)
setq
不声明变量,而是设置变量。2) 它还需要两个参数,而不是一个。3)
SETQ
是一个特殊运算符,需要一个符号作为其第一个参数。你提供了一份清单。4)
+
要求数字作为参数,而不是符号。第五个错误:
(dotimes(in)(setq(+'ni))
:缺少括号。实际上,为什么不声明一个n长度的向量呢?不过,正如雷纳所建议的,在掌握正常编程之前,您可能会推迟元编程:)四个错误:
(setq(+'ni))
:1)
setq
不声明变量,而是设置变量。2) 它还需要两个参数,而不是一个。3)
SETQ
是一个特殊运算符,需要一个符号作为其第一个参数。你提供了一份清单。4)
+
要求数字作为参数,而不是符号。第五个错误:
(dotimes(in)(setq(+'ni))
:缺少括号。实际上,为什么不声明一个n长度的向量呢?不过,正如雷纳所建议的,在掌握正常编程之前,您可能会推迟元编程:)四个错误:
(setq(+'ni))
:1)
setq
不声明变量,而是设置变量。2) 它还需要两个参数,而不是一个。3)
SETQ
是一个特殊运算符,需要一个符号作为其第一个参数。你提供了一份清单。4)
+
要求数字作为参数,而不是符号。第五个错误:
(dotimes(in)(setq(+'ni))
:缺少括号。实际上,为什么不声明一个n长度的向量呢?不过,正如雷纳所建议的,在掌握正常编程之前,您可能会推迟元编程:)四个错误:
(setq(+'ni))
:1)
setq
不声明变量,而是设置变量。2) 它还需要两个参数,而不是一个。3)
SETQ
是一个特殊运算符,需要一个符号作为其第一个参数。你提供了一份清单。4)
+
要求数字作为参数,而不是符号。第五个错误:
(dotimes(in)(setq(+'ni))
:缺少括号。实际上,为什么不声明一个n长度的向量呢?不过,正如雷纳所建议的,在掌握正常编程之前,您可能会推迟元编程:)