0

関数で配列を作成し、その関数から呼び出される別の関数にパラメーターとして渡したいと思います。どうやってやるの?擬似コードは次のとおりです。

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

1 に答える 1

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)

パラメータが期待されるタイプであることを確認したい場合はtypep、 、type-oftypecaseまたはを使用できます。check-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).
于 2014-10-29T08:13:59.497 に答える