基本方案递归

发布于 2024-10-19 04:04:15 字数 1532 浏览 1 评论 0原文

我有一个这样的文件:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还不是爱你 2024-10-26 04:04:15

我已经为您修复了代码格式,这揭示了问题所在:(eq? token 'begin) 周围的括号层太多。固定版本如下所示:

(define declarations
  (lambda (token)
    (cond ((eq? token 'begin) '())
          (else (let* ((name token)
                       (eqsign (read))
                       (value (read)))
                  (cons (list name value) (declarations (read))))))))

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:

(define declarations
  (lambda (token)
    (cond ((eq? token 'begin) '())
          (else (let* ((name token)
                       (eqsign (read))
                       (value (read)))
                  (cons (list name value) (declarations (read))))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文