2013-04-28 64 views
0
(define input (read-line)) 
(define lngth (string-length input)) 
(define first-char (string-ref input 0)) 
(define test-num 
    (string->number 
    (substring input 0 (- (string-search-forward " " input) 1)))) 
(define end (string-search-forward ")" input)) 
(define beginning (string-search-backward "(" input)) 
(define operation (string (substring input beginning (+ end 1)))) 
(define space1 (string-search-forward " " operation)) 
(define space2 (string-search-backward " " operation)) 
(define n1 (string->number (substring operation 1 space1))) 
(define n2 (string->number (substring operation (+ space1 1) space2))) 
(define result (1)) 
(define operator (substring operation (+ space2 1) end)) 

(if (or (equal? first-char #\() (number? test-num)) 
    (display "yay") 
    (display "ERROR")) 

我在MIT/GNU Scheme實現中使用它,它讓我運行這段代碼並正確響​​應。我的問題是,一旦用戶數據被放入函數中,它不會將任何類型的信息綁定到變量。 I.E.投入數據,並試圖訪問任何我得到的參數後,執行以下操作:計劃中的未綁定變量

1 ]=> (display input) 
;Unbound variable : input 
+2

你是如何「跑步」的代碼?很可能你正在以某種方式運行它,在一個環境中評估表單,然後當你需要訪問變量時,在評估環境中找不到它們。 – GoZoner 2013-04-28 21:44:02

+0

我將它作爲.scm保存在文本文件中,然後直接用MIT/GNU Scheme解釋器打開它。 – 2013-04-28 22:07:17

+0

你說'(load file.scm)',然後加載等待你鍵入一些東西(因爲你叫'read-line')?或者做其他事情發生? – GoZoner 2013-04-28 22:21:22

回答

0

對不起,對我的作品:

$ mit-scheme 
MIT/GNU Scheme running under MacOSX 
Type `^C' (control-C) followed by `H' to obtain information about interrupts. 

Copyright (C) 2011 Massachusetts Institute of Technology 
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. 

Image saved on Tuesday November 8, 2011 at 10:45:46 PM 
    Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118 || Edwin 3.116 

1 ]=> (load "bar.scm") 

;Loading "bar.scm"...(a b c)   ;; (define input (read-line)) 
done 
;Value: len 

1 ]=> input       ;; 'input' is bound 

;Value 13: "(a b c)" 

1 ]=> ^D 
End of input stream reached. 
Moriturus te saluto. 
+0

這很奇怪。我會嘗試在另一臺計算機上或其他方面。無論如何,非常感謝您的幫助。 – 2013-04-28 23:06:56