2014-10-29 54 views
0

我想在一个函数中创建一个数组并将其作为参数传递给另一个函数,该函数将从该函数调用。我怎样才能做到这一点?下面是伪代码:如何定义一个将数组作为参数的LISP函数?

define FuncA (Array Q){ 
    <whatever> 
} 

define FuncB (n){ 
    make-array myArray = {0,0,....0}; <initialise an array of n elements with zeroes> 
    FuncA(myArray); <call FuncA from FuncB with myArray as an argument> 
} 
+2

假如你尝试过的东西,建议你需要做一些特定的事情来接受一个数组? – 2014-10-29 10:17:18

回答

9

Common Lisp是动态类型,所以数组参数被以同样的方式与任何其他参数中声明,没有它的类型:

(defun funcA (Q) 
    Q) ; just return the parameter 

(defun funcB (n) 
    (let ((arr (make-array n :initial-element 0))) 
    (funcA arr))) 

,或者,如果你不吨需要创建绑定,只需

(defun funcB (n) 
    (funcA (make-array n :initial-element 0))) 

测试

? (funcB 10) 
#(0 0 0 0 0 0 0 0 0 0) 

如果要检查参数是预期的类型,你可以使用typeptype-oftypecasecheck-type,例如:

(defun funcA (Q) 
    (check-type Q array) 
    Q) 

然后

? (funcA 10) 
> Error: The value 10 is not of the expected type ARRAY. 
> While executing: FUNCA, in process Listener(4). 
相关问题