サーチ…


備考

匿名関数は、 LAMBDAを使用して作成できます。ローカル関数は、 LABELSまたはFLETを使用して定義できます。それらのパラメータは、グローバル名付き関数と同じように定義されます。

必要なパラメータ

(defun foobar (x y)
  (format t "X: ~s~@
             Y: ~s~%"
          x y))

(foobar 10 20)
; X: 10
; Y: 20
;=> NIL

オプションのパラメータ

オプションのパラメーターは、 &OPTIONALキーワードを使用して、必要なパラメーターの後に指定することができます。後に複数のオプションのパラメータが存在する可能性があります。

(defun foobar (x y &optional z)
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is optional.~%"
          x y z))

(foobar 10 20)
; X (10) and Y (20) are required.
; Z (NIL) is optional.
;=> NIL
(foobar 10 20 30)
; X (10) and Y (20) are required.
; Z (30) is optional.
;=> NIL

デフォルト値

オプションのパラメータには、リストでパラメータを指定することによってデフォルト値を与えることができます。 2番目の値がデフォルトです。デフォルト値フォームは、引数が与えられている場合にのみ評価されるため、エラーの通知などの副作用に使用できます。

(defun foobar (x y &optional (z "Default"))
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is optional.~%"
          x y z))

(foobar 10 20)
; X (10) and Y (20) are required.
; Z ("Default") is optional.
;=> NIL
(foobar 10 20 30)
; X (10) and Y (20) are required.
; Z (30) is optional.
;=> NIL

オプション引数が指定されているかどうかを確認する

デフォルト値の後に3番目のメンバーをリストに追加することができます。引数が指定された場合はtrue、それが指定されていない場合はNIL (デフォルトが使用されます)の変数名。

(defun foobar (x y &optional (z "Default" zp))
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is optional. It ~:[wasn't~;was~] given.~%"
          x y z zp))

(foobar 10 20)
; X (10) and Y (20) are required.
; Z ("Default") is optional. It wasn't given.
;=> NIL
(foobar 10 20 30)
; X (10) and Y (20) are required.
; Z (30) is optional. It was given.
;=> NIL

パラメータなしの関数

グローバルな名前付き関数はDEFUN定義されていDEFUN

(defun foobar ()
  "Optional documentation string. Can contain line breaks.

Must be at the beginning of the function body. Some will format the
docstring so that lines are indented to match the first line, although
the built-in DESCRIBE-function will print it badly indented that way.

Ensure no line starts with an opening parenthesis by escaping them
\(like this), otherwise your editor may have problems identifying
toplevel forms."
  (format t "No parameters.~%"))

(foobar)
; No parameters.
;=> NIL

(describe #'foobar) ; The output is implementation dependant.
; #<FUNCTION FOOBAR>
;   [compiled function]
;
; Lambda-list: ()
; Derived type: (FUNCTION NIL (VALUES NULL &OPTIONAL))
; Documentation:
;   Optional documentation string. Can contain line breaks.
;   
;   Must be at the beginning of the function body. Some will format the
;   docstring so that lines are indented to match the first line, although
;   the built-in DESCRIBE-function will print it badly indented that way.
; Source file: /tmp/fileInaZ1P
;=> No values

関数本体は、任意の数のフォームを含むことができます。最後のフォームからの値が関数から返されます。

残りパラメータ

必要な引数の後に&REST restというキーワードを付けてrestパラメータを1つ与えることができます。そのようなパラメータが存在する場合、関数はいくつかの引数を取ることができ、それらはrest-parameterのリストにグループ化されます。変数CALL-ARGUMENTS-LIMITは、関数呼び出しで使用できる引数の最大数を決定するため、引数の数は実装固有の最小50個以上の引数に制限されています。

(defun foobar (x y &rest rest)
  (format t "X (~s) and Y (~s) are required.~@
             The function was also given following arguments: ~s~%"
          x y rest))

(foobar 10 20)
; X (10) and Y (20) are required.
; The function was also given following arguments: NIL
;=> NIL
(foobar 10 20 30 40 50 60 70 80)
; X (10) and Y (20) are required.
; The function was also given following arguments: (30 40 50 60 70 80)
;=> NIL

残りのキーワードとキーワードのパラメータ

残りのパラメータは、キーワードパラメータの前に置くことができます。その場合、ユーザーが指定したプロパティリストが含まれます。キーワード値は、対応するキーワードパラメータに依然としてバインドされます。

(defun foobar (x y &rest rest &key (z 10 zp))
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~@
             The function was also given following arguments: ~s~%"
          x y z zp rest))

(foobar 10 20)
; X (10) and Y (20) are required.
; Z (10) is a keyword argument. It wasn't given.
; The function was also given following arguments: NIL
;=> NIL
(foobar 10 20 :z 30)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
; The function was also given following arguments: (:Z 30)
;=> NIL

キーワード&ALLOW-OTHER-KEYSは、パラメータとして定義されていないキーワード引数をユーザーが指定できるように、lambda-listの最後に追加できます。彼らは残りのリストに入るでしょう。

(defun foobar (x y &rest rest &key (z 10 zp) &allow-other-keys)
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~@
             The function was also given following arguments: ~s~%"
          x y z zp rest))

(foobar 10 20 :z 30 :q 40)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
; The function was also given following arguments: (:Z 30 :Q 40)
;=> NIL

補助変数

&AUXキーワードを使用して、関数のローカル変数を定義することができます。それらはパラメータではありません。ユーザはそれらを供給することができない。

&AUX変数はめったに使用されません。代わりにLETを使うか、関数本体にローカル変数を定義する他の方法を使うことができます。

&AUX変数には、関数本体全体のローカル変数が先頭に移動し、(例えばLETによって導入された)1つのインデントレベルが不要になるという利点があります。

(defun foobar (x y &aux (z (+ x y)))
  (format t "X (~d) and Y (~d) are required.~@
             Their sum is ~d."
          x y z))

(foobar 10 20)
; X (10) and Y (20) are required.
; Their sum is 30.
;=> NIL

1つの典型的な使用法は、「指定子」パラメータを解決することである。繰り返しますが、このようにする必要はありません。 letを使うletはちょうど慣用的です。

(defun foo (a b &aux (as (string a)))
  "Combines A and B in a funny way.  A is a string designator, B a string."
  (concatenate 'string as " is funnier than " b))

RETURN-FROM、ブロックまたは関数を終了する

関数は常にボディの周りにブロックを設定します。このブロックは関数名と同じ名前です。つまり、このブロック名でRETURN-FROMを使用して関数から戻り、値を返すことができます。

可能な限り早めに返却しないでください。

(defun foobar (x y)
  (when (oddp x)
    (format t "X (~d) is odd. Returning immediately.~%" x)
    (return-from foobar "return value"))
  (format t "X: ~s~@
             Y: ~s~%"
          x y))

(foobar 10 20)
; X: 10
; Y: 20
;=> NIL
(foobar 9 20)
; X (9) is odd. Returning immediately.
;=> "return value"

キーワードパラメータ

キーワード・パラメータは、 &KEYキーワードで定義できます。これらは常にオプションです(定義の詳細については、オプションのパラメータの例を参照してください)。複数のキーワードパラメータが存在する可能性があります。

(defun foobar (x y &key (z "Default" zp))
  (format t "X (~s) and Y (~s) are required.~@
             Z (~s) is a keyword argument. It ~:[wasn't~;was~] given.~%"
          x y z zp))

(foobar 10 20)
; X (10) and Y (20) are required.
; Z ("Default") is a keyword argument. It wasn't given.
;=> NIL
(foobar 10 20 :z 30)
; X (10) and Y (20) are required.
; Z (30) is a keyword argument. It was given.
;=> NIL


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow