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
你是如何「跑步」的代碼?很可能你正在以某種方式運行它,在一個環境中評估表單,然後當你需要訪問變量時,在評估環境中找不到它們。 – GoZoner 2013-04-28 21:44:02
我將它作爲.scm保存在文本文件中,然後直接用MIT/GNU Scheme解釋器打開它。 – 2013-04-28 22:07:17
你說'(load file.scm)',然後加載等待你鍵入一些東西(因爲你叫'read-line')?或者做其他事情發生? – GoZoner 2013-04-28 22:21:22