基本方案递归
我有一个这样的文件:
declare
a = aexpress
b = bexpress
begin
我的方案程序将当前输入端口设置为该文件,然后调用 (声明(读取))
我返回的是#f
。 或者更确切地说,控制台显示“对象 #f 不适用。”
我已经结束了括号的使用,找不到任何它应该返回布尔值的原因,但我确信我错过了一些东西。
我想要的是 ((a aexpress) (b bexpress))
(define declarations
(lambda (token)
(cond (((eq? token 'begin) '())
(else (let* ((name token)
(eqsign (read))
(value (read)))
(cons (list name value) (declarations (read)))))))))
调用者:
(define convert
(lambda (filename)
(begin
(set-current-input-port! (open-input-file filename))
(statement (read))
)
)
)
(define statement (lambda (token) (
cond (
( (eq? token 'declare) (declarations (read)) )
; ( (eq? token 'declare) (declare_statement) )
; ( (eq? token 'begin) (begin_statement) )
; ( (eq? token 'for) (for_statement) )
; ( (eq? token 'if) (if_statement) )
; ( (eq? token 'set) (set_statement) )
; (else (expression_token))
))))
I have a file like this:
declare
a = aexpress
b = bexpress
begin
My scheme program sets the current input port to this file then calls(declarations (read))
What im getting back, is #f
.
or rather the console says "The object #f is not applicable."
I have been over my parenthesis use and cant find any reason it should be returning a boolean value, but I'm sure I'm missing something.
What I want, is ((a aexpress) (b bexpress))
(define declarations
(lambda (token)
(cond (((eq? token 'begin) '())
(else (let* ((name token)
(eqsign (read))
(value (read)))
(cons (list name value) (declarations (read)))))))))
Called by:
(define convert
(lambda (filename)
(begin
(set-current-input-port! (open-input-file filename))
(statement (read))
)
)
)
(define statement (lambda (token) (
cond (
( (eq? token 'declare) (declarations (read)) )
; ( (eq? token 'declare) (declare_statement) )
; ( (eq? token 'begin) (begin_statement) )
; ( (eq? token 'for) (for_statement) )
; ( (eq? token 'if) (if_statement) )
; ( (eq? token 'set) (set_statement) )
; (else (expression_token))
))))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经为您修复了代码格式,这揭示了问题所在:
(eq? token 'begin)
周围的括号层太多。固定版本如下所示:I've fixed the code formatting for you, which reveals what the problem is: you have too many layers of brackets around the
(eq? token 'begin)
. The fixed version would look like this: