खोज…


टिप्पणियों

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

डिफ़ॉल्ट रूप से

एक सूची के साथ पैरामीटर निर्दिष्ट करके वैकल्पिक मापदंडों के लिए एक डिफ़ॉल्ट मान दिया जा सकता है; दूसरा मान डिफ़ॉल्ट है। डिफ़ॉल्ट मान फ़ॉर्म का मूल्यांकन केवल तभी किया जाएगा जब तर्क दिया गया था, इसलिए इसका उपयोग साइड-इफेक्ट के लिए किया जा सकता है, जैसे कि त्रुटि का संकेत देना।

(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

यदि वैकल्पिक तर्क दिया गया था, तो जांचें

डिफ़ॉल्ट मान के बाद एक तीसरे सदस्य को सूची में जोड़ा जा सकता है; एक चर नाम जो कि तर्क दिए जाने पर सत्य है, या 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 साथ एक सिंगल रेस्ट-पैरामीटर दिया जा सकता है। यदि ऐसा कोई पैरामीटर मौजूद है, तो फ़ंक्शन कई तर्क ले सकता है, जिसे बाकी पैरामीटर में एक सूची में वर्गीकृत किया जाएगा। ध्यान दें कि चर 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 को लैम्बडा-सूची के अंत में जोड़ा जा सकता है, ताकि उपयोगकर्ता को उपयोगकर्ता के तर्कों को मापदंडों के रूप में परिभाषित नहीं किया जा सके। वे बाकी सूची में जाएंगे।

(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 चर के फायदे हैं कि पूरे फ़ंक्शन बॉडी के स्थानीय चर शीर्ष पर चले जाते हैं और यह एक इंडेंटेशन स्तर (उदाहरण के लिए एलईटी द्वारा पेश किया गया) को अनावश्यक बनाता है।

(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

एक विशिष्ट उपयोग "डिज़ाइनर" मापदंडों को हल कर सकता है। फिर, आपको इसे इस तरह से करने की आवश्यकता नहीं है; 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