2014-09-29 50 views
0

我想讓我的Scheme程序導入字符串,而不需要在字符串之前使用open-input-string。因此,例如,現在我可以做到以下幾點:將字符串導入Scheme而不使用開放輸入字符串

> (scheme_lexer (open-input-string "3+4*2")) 

但是,是有我的程序協同工作的方式,如果我輸入字符串這樣?:

> (scheme_lexer ("3+4*2")) 

謝謝!

+0

如果有任何答案適合您。請接受它。 – Rptx 2014-10-01 15:37:50

回答

1

有什麼特別的原因,你不能只是做一個scheme_lexer_string函數處理字符串打交道時,這是否適合你?額外的括號看起來很混亂,而且它們是宏觀唯一真正的解決方案。如果丟棄的要求,並做出類似(scheme_lexer "3+4*2")可以接受的,你可以讓一個普通的功能,用於處理字符串:

(define (scheme_lexer_string s) 
    (scheme_lexer (open-input-string s))) 

如果你想要的是處理兩個輸入端口和字符串的函數,你可以做一個一般功能根據特定函數的參數類型進行分派。在這種情況下,原來的scheme_lexer將被重新命名爲scheme_lexer_input_port,你將有以下功能:

(define (scheme_lexer_string s) 
    (scheme_lexer_input_port (open-input-string s))) 

(define (scheme_lexer in) 
    (if (string? in) 
     (scheme_lexer_string in) 
     (scheme_lexer_input_port in))) 

現在scheme_lexer作品字符串和端口,並分發到正確的功能所需。

> (scheme_lexer some-input-port) 
... evaluates the content in the port 
> (scheme_lexer "abcd") 
... evaluates the string "abcd" 
+0

這工作。非常感謝! – jean28 2014-10-12 07:49:38

0

這裏有一個選項。我已經使用測試功能lexer只是爲了顯示宏。您可以根據自己的需求進行調整。

(define (lexer sp) (read sp)) 

(define-syntax scheme_lexer 
    (syntax-rules() 
    ((_ (input)) 
    (lexer (open-input-string input))))) 

,並測試:

> (scheme_lexer ("3+4*2")) 
'3+4*2