2010-10-26 20 views
6

समझा नहीं गया है मैं समय-हत्या परियोजना के रूप में लिस्प में एक अनुमान लगाने वाला गेम लिखने की कोशिश कर रहा हूं। हालांकि, जब मैं का उपयोग कर SBCL, मैं निम्नलिखित त्रुटि मिलती कार्यक्रम को लोड करने का प्रयास:सामान्य लिस्प त्रुटि

debugger invoked on a SB-C::INPUT-ERROR-IN-COMPILE-FILE in thread #<THREAD 
                    "initial thread" RUNNING 
                    {AA14959}>: 
    READ failure in COMPILE-FILE at character 477: 
    end of file on #<SB-SYS:FD-STREAM 
        for "file /home/andy/Dropbox/Programming/Common Lisp/number-game.lisp" 
        {B4F45F9}> 

Type HELP for debugger help, or (SB-EXT:QUIT) to exit from SBCL. 

restarts (invokable by number or by possibly-abbreviated name): 
    0: [CONTINUE] Ignore runtime option --load "number-game.lisp". 
    1: [ABORT ] Skip rest of --eval and --load options. 
    2:   Skip to toplevel READ/EVAL/PRINT loop. 
    3: [QUIT ] Quit SBCL (calling #'QUIT, killing the process). 

(SB-C::READ-FOR-COMPILE-FILE 
#<SB-SYS:FD-STREAM 
    for "file /home/andy/Dropbox/Programming/Common Lisp/number-game.lisp" 
    {B4F45F9}> 
477) 

क्या इस त्रुटि क्या मतलब है? कोड इस प्रकार है, और जब फ़ाइल लोड और आरईपीएल से (play) बुला त्रुटि दिखाई देती है:

;;;; number-game.lisp 
;;;; 
;;;; Andrew Levenson 
;;;; 10/25/2010 
;;;; 
;;;; Simple number guessing game. User has 
;;;; five guesses to determine a number between 
;;;; one and one hundred, inclusive (1-100). 

;;; Set global variable for the target number: 
(defparameter *target* nil) 

;;; Set the iterator so we may check the number of guesses 
(defparameter *number-of-guesses* 0) 

;;; Welcome the user 
(defun welcome-user() 
    (format t "Welcome to the number guessing game!~%")) 

;;; Prompt for a guess 
(defun prompt-for-guess() 
    (format t "Please enter your guess (1-100): ") 
    (finish-output nil) ; nil directs finish-output to standard IO 
    (check-guess((read-guess))) 

;;; Read in a guess 
(defun read-guess() 
    (let ((guess (read))) 
     (if (numberp guess) ; If true, return guess. Else, call prompt-for-guess 
      (progn 
       (setq *number-of-guesses* (+ *number-of-guesses* 1)) 
       guess) 
      (prompt-for-guess)))) 

;;; Check if the guess is higher than, lower than, or equal to, the target 
(defun check-guess (guess) 
    (if (equal guess *target*) 
     (equal-to) 
     (if (> guess *target*) 
      (greater-than (guess)) 
      (if (< guess *target*) 
       (less-than (guess)))))) 

;;; If the guess is equal to the target, the game is over 
(defun equal-to() 
    (format t "Congratulations! You have guessed the target number, ~a!~%" *target*) 
    (y-or-n-p "Play again? [y/n] ")) 

;;; If the guess is greater than the target, inform the player. 
(defun greater-than (guess) 
    (format t "Sorry, ~a is greater than the target.~%" guess) 
    (if (< *number-of-guesses* 6) 
     (prompt-for-guess) 
     (game-over))) 

;;; If the guess is less than the target, inform the player. 
(defun less-than (guess) 
    (format t "Sorry, ~a is less than the target.~%" guess) 
    (if (< *number-of-guesses* 6) 
     (prompt-for-guess) 
     (game-over))) 

;;; If the player has run out of guesses, give them the option 
;;; of playing the game again. 
(defun game-over() 
    (y-or-n-p "You have run out of guesses. Play again? [y/n] ")) 


;;; Play the game 
(defun play() 
    ;; If it's their first time playing this session, 
    ;; make sure to greet the user. 
    (unless (> *number-of-guesses* 0) 
     (welcome-user)) 
    ;; Reset their remaining guesses 
    (setq *number-of-guesses* 0) 
    ;; Set the target value 
    (setq *target* 
     ;; Random can return float values, 
     ;; so we must round the result to get 
     ;; an integer value. 
     (round 
      ;; Add one to the result, because 
      ;; (random 100) yields a number between 
      ;; 0 and 99, whereas we want a number 
      ;; from 1 to 100 inclusive. 
      (+ (random 100) 1))) 
    (if (equal (prompt-for-guess) "y") 
     (play) 
     (quit))) 

(मैं काफी हद तक निश्चित है कि कार्यक्रम काम नहीं करता हूँ शून्य से है कि एक त्रुटि, मैं अभी भी कर रहा हूँ एक पूरी नौसिखिए जब यह लिस्प के लिए आता है। यह सिर्फ पहली त्रुटि मैं का सामना करना पड़ा है कि मैं अपने दम पर को समझ नहीं सकता है।)

ओह, और इस मुद्दे को सबसे अधिक संभावना prompt-for-guess, read-guess के साथ क्या करना है और check-guess फ़ंक्शंस, क्योंकि वे थे जब मैं इस त्रुटि को फसल कर रहा था।

उत्तर

6

ऐसा लगता है कि आपने अपने prompt-for-guess डिफ्यून पर पर्याप्त पैरों को बंद नहीं किया है।

पाठक फ़ाइल के अंत तक पहुंच रहा है और यह देखते हुए कि इसका एक फॉर्म अभी भी खुला है, और यह पता नहीं लगा सकता कि यह कहां से है।

इस तरह की त्रुटियों को खोजने के लिए मैं उपयोग करने का एक आसान तरीका यह है कि मेरा टेक्स्ट एडिटर इस क्षेत्र को इंडेंट करे, और सुनिश्चित करें कि सब कुछ लाइनें जैसे मुझे लगता है कि यह चाहिए। दौरान फ़ाइल की

+0

बहुत बढ़िया, यह उस त्रुटि को ठीक कर दिया! धन्यवाद! अब मुझे यह समझने की जरूरत है कि फ़ंक्शन के रिटर्न वैल्यू को किसी अन्य फ़ंक्शन के तर्क के रूप में कैसे पास किया जाए। :) – Andy

+2

@ एंड्रयू: '(अन्य फ़ंक्शन (प्रथम-फ़ंक्शन))'? –

1

अंत पढ़ा, वहाँ एक समापन कोष्ठक (या समान) याद आ रही है। कैरेक्टर 477. कर्सर को अपने टेक्स्ट में 477 पर ले जाएं और जांचें कि यह कौन सी अभिव्यक्ति है।

असंतुलित अभिव्यक्तियों को खोजने के लिए अपने आईडीई की जांच करें।

लिस्पॉर्क्स में यह एम-एक्स असंतुलित पेंटिचेस मिलेगा।

एसएलआईएमई के लिए भी कुछ कमांड होना चाहिए।

3

एमएक्स में कमांड एम-एक्स चेक-पैनस (यह सबकुछ जांचता है जो संतुलन की तरह, संतुलन की तरह) की जांच करता है। अगर सब कुछ संतुलित हो जाता है, तो यह थोड़ा रहस्यमय हो सकता है, क्योंकि यह उस मामले में कुछ भी नहीं करता है।

चेक-पैर कमांड: वर्तमान बफर में असंतुलित कोष्ठक के लिए जांचें। अधिक सटीक रूप से, असंतुलित अभिव्यक्तियों ("sexps") के लिए सामान्य रूप से बफर के संकुचित भाग को जांचें। यह वर्तमान वाक्यविन्यास तालिका के अनुसार किया जाता है और उचित के रूप में असंतुलित ब्रैकेट या उद्धरण पाएगा। (जानकारी नोड `(emacs) Parentheses देखें।) यदि असंतुलन पाया गया है, तो एक त्रुटि संकेतित की जाती है और बिंदु पहले असंतुलित वर्ण पर छोड़ा जाता है।

संबंधित मुद्दे