2010-10-26 23 views
6

Sto provando a scrivere un gioco di indovinelli in Lisp come un progetto che uccide il tempo. Tuttavia, quando cerco di caricare il programma utilizzando SBCL, ottengo il seguente errore:Errore Lisp comune non rilevato

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) 

Che cosa significa questo errore? Il codice è il seguente, e l'errore si verifica quando si carica il file e chiamando (play) dal REPL:

;;;; 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))) 

(io sono abbastanza certo che il programma non funziona meno che un errore, io sono ancora un novizio completo quando si tratta di Lisp. Questo è solo il primo errore che ho riscontrato che non riesco a capire da solo.)

Oh, e il problema molto probabilmente ha a che fare con lo prompt-for-guess, read-guess e le funzioni check-guess, perché quelle erano quelle con cui stavo scherzando quando si è verificato questo errore.

risposta

6

Sembra che tu non abbia chiuso abbastanza paren sul tuo prompt-for-guess defun.

Il lettore sta arrivando alla fine del file e ha notato che ha una forma ancora aperta e non riesce a capire da dove proviene.

Un modo semplice che utilizzo per trovare errori di questo tipo è di fare in modo che il mio editor di testo indentizzi la regione e assicurarsi che tutto si allinei come penso che dovrebbe.

+0

Impressionante, ha corretto quell'errore! Grazie! Ora ho bisogno di capire come passare il valore di ritorno di una funzione come argomento di un'altra funzione. :) – Andy

+2

@Andrew: '(other-function (first-function))'? –

1

fine del file durante leggere, c'è una parentesi di chiusura (o simile) mancante. Carattere 477. Spostare il cursore nel testo su 477 e verificare quale espressione sia.

Controllare l'IDE per un comando per trovare espressioni non bilanciate.

In LispWorks questo sarebbe M-x Trova parentesi squilibrate.

SLIME dovrebbe avere qualche comando anche per questo.

3

Il comando in Emacs è M-x check-parens (controlla tutto ciò che deve essere bilanciato, come le virgolette). Può essere un po 'mistificante se tutto si equilibra, perché in questo caso non fa nulla.

check-parens Comando: verifica le parentesi sbilanciate nel buffer corrente. Più precisamente, controllare la parte ristretta del buffer per espressioni squilibrate ("sexps") in generale. Questa operazione viene eseguita in base alla tabella di sintassi corrente e troverà parentesi o virgolette non bilanciate come appropriate. (Vedi Info node `(emacs) Parentheses '.) Se lo squilibrio è trovato , viene segnalato un errore e il punto viene lasciato al primo carattere squilibrato .

Problemi correlati