2011-10-12 10 views
5

हैलो में है, तो हैलो मैं lisp में घोंसला बनाने की कोशिश कर रहा हूं, लेकिन हम त्रुटि प्राप्त करते रहते हैं, और हम नहीं जानते कि इसे कैसे ठीक किया जाए!अगर lisp

** - EVAL: विशेष ऑपरेटर के लिए भी कई मापदंडों यदि:

(defun spread-stones-helper(game-state StoneInHand Player player-index pit-index) 

    ;; Do we have more stones in our hand? 
    (if (> 0 StoneInHand) 
     ;; Are we above the pit limit? 
     (if (> pit-index 5) 
      ;; Switch the player and reset the pit-index to 0 
      (setq player-index (switchplayer player-index)) 
      (setq pit-index '0) 
     ) 

     ;; Add 1 to the pit 
     (set-pit game-state player-index (GetCorrectPit player-index pit-index) (+ (get-pit game-state player-index (GetCorrectPit player-index pit-index)) 1)) 

     ;; Recursive call the function, with one less stone and 1 up in pit-index 
     (spread-stones-helper game-state (- StoneInHand 1) Player player-index (+ pit-index 1)) 
    ) 
    ;; There are no more stones in hand, run capture stones 
    ;; (captureStones game-state StoneInHand Player player-index pit-index) 
) 
+0

मुझे लगता है कि आप शायद अपने आप को अजीब माता-पिता/इंडेंटेशन शैली के साथ भ्रमित कर रहे हैं। – Ken

उत्तर

8

if ऑपरेटर लिस्प में की जरूरत है तीन भाव कि हालत, मूल्य के मामले में हालत सही है और मान रहे हैं जब हालत झूठी है स्वीकार करता है .. उदाहरण के लिए

(if (< x 0) 
    (print "x is negative") 
    (print "x is greater or equal than zero")) 

आप अंतिम अभिव्यक्ति को भी छोड़ सकते हैं और इस मामले में यह शून्य माना जाता है।

आप दो मामलों में से एक में अधिक भाव रखना चाहते हैं, तो आप उन्हें एक progn रूप

(if (< x 0) 
    (progn 
     (print "HEY!!!!") 
     (print "The value of x is negative..."))) 

केवल दो शाखाओं में से एक भरा साथ और कई के साथ एक if अभिव्यक्ति होने के मामले में लपेट चाहिए भाव बहुत लगातार हो पाया था और इसलिए दो इस सटीक उपयोग के लिए विशेष रूपों जोड़ा गया:

(when (< x 0) 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing)) 

(unless (< x 0) 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing)) 

ऊपर when रूप

के बराबर है
(if (< x 0) 
    (progn 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing))) 

unless रूप बहुत एक ही अर्थ है, लेकिन शर्त के साथ उलट ... दूसरे शब्दों में यह सारांश यह है कि आप if का उपयोग करना चाहिए केवल जब तुम के लिए कोड लिखने की ज़रूरत

(if (not (< x 0)) 
    (progn 
    (do-this) 
    (do-that) 
    (do-even-that-other-thing))) 

के बराबर है शाखाएं (सत्य और झूठी एक) दोनों। अन्यथा अपने परीक्षण के लिए और अधिक पढ़ने योग्य के आधार पर when या unless का उपयोग करें।

if फ़ॉर्म का उपयोग करते समय आपको शाखाओं में progn का उपयोग करना होगा जहां आपको एक से अधिक फॉर्म डालना होगा।

5

एक से अधिक के लिए (progn ...) उपयोग करने के लिए मत भूलना अगर बयान

(defun spread-stones-helper (game-state StoneInHand Player 
          player-index pit-index) 

    ;; Do we have more stones in our hand? 
    (if (> 0 StoneInHand) 
     (progn 
     ;; Are we above the pit limit? 
     (if (> pit-index 5) 
     (progn 
       ;; Switch the player and reset the pit-index to 0 
       (setq player-index (switchplayer player-index)) 
       (setq pit-index '0))) 

     ;; Add 1 to the pit 
     (set-pit game-state player-index 
        (GetCorrectPit player-index pit-index) 
        (+ (get-pit game-state player-index 
           (GetCorrectPit player-index pit-index)) 
        1)) 

     ;; Recursive call the function, with one less stone and 1 
     ;; up in pit-index 
     (spread-stones-helper game-state 
           (- StoneInHand 1) 
           Player 
           player-index 
           (+ pit-index 1)))) 
    ;; There are no more stones in hand, run capture stones 
    ;; (captureStones game-state StoneInHand Player player-index pit-index) 
    ) 
5

" अगर "एक परीक्षण और दो रूप लेता है -

आपने पहले" अगर "परीक्षण और तीन दिया है फॉर्म

मान लें (> 0 StoneInHand) सत्य है।

क्या आप दोनों सेकंड और सेट-पिट स्टेटमेंट्स को चलाने के लिए चाहते हैं?

यदि हां, तो आप उन्हें एक (progn) में लपेट के लिए