LISP: Keyword parameters, supplied-p

前端 未结 2 2033
误落风尘
误落风尘 2021-02-20 15:21

At the moment I\'m working through \"Practical Common Lisp\" from Peter Seibel.

In the chapter \"Practical: A Simple Database\" (http://www.gigamonkeys.com/book/pract

相关标签:
2条回答
  • 2021-02-20 15:43

    It's difficult to tell what you are asking. c-p is bound to T or NIL, depending on whether c is supplied as a parameter. This binding is then available to the body of the function.

    0 讨论(0)
  • 2021-02-20 15:46

    Let's reindent the function foo:

    (defun foo (&key a
                     (b 20)
                     (c 30 c-p))
       (list a b c c-p))
    

    If you indent it like this you will see that the function has three keyword parameters: a, b and c. These are available in the body of the function.

    For the keyword parameter c there is a variable declared c-p that will be T or NIL depending whether c has been passed when foo gets called.

    A keyword parameter generally can be declared as one of the following options:

    1. as a single variable name
    2. a list of a variable name and a default value
    3. a list of a variable name, a default value and a variable that will show whether the parameter has been passed or not when the function gets called

    The supplied-p is particularly interesting when one wants to see whether the value comes from the call or the default value:

    (defun make-my-array (size &key (init-value nil init-value-supplied-p))
       (if init-value-supplied-p
           (make-array size :initial-element init-value)
           (make-array size)))
    

    Now the user can init the elements to NIL:

    (make-my-array 10 :init-value nil)
    

    Here the default value and the supplied value can both be NIL, but we need to make a difference. The variable init-value-supplied-p makes it possible to see whether the NIL value of the variable init-value comes from the default or from the function call.

    0 讨论(0)
提交回复
热议问题