【问题标题】:How can I define a LISP function, which takes in an array as an argument?如何定义一个将数组作为参数的 LISP 函数?
【发布时间】:2014-10-29 07:22:09
【问题描述】:

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

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>
}

【问题讨论】:

  • 您是否尝试过一些建议您需要做一些特定的事情来接受数组?

标签: lisp common-lisp lispworks


【解决方案1】:

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).

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多