recursion - 基本方案递归

标签 recursion lisp scheme

我有这样一个文件:

    declare
    a = aexpress 
    b = bexpress 
    begin 

我的方案程序将当前输入端口设置为该文件然后调用 (声明(阅读)) 我得到的是 #f。 或者更确切地说,控制台显示“对象 #f 不适用。”

我已经结束了括号的使用,找不到它应该返回 bool 值的任何理由,但我确定我遗漏了一些东西。

我想要的是 ((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))


                                   ))))

最佳答案

我已经为您修复了代码格式,这揭示了问题所在:(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))))))))

关于recursion - 基本方案递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5129777/

相关文章:

lisp - mit 方案 vim slimv : "read-error: no dispatch function defined for #\F"

recursion - 更适合Common Lisp抽象来实现 "self recursive let"

lisp - 在 Lisp 中进行(严肃的)Web 开发是否可行?

lisp - 劣质外壳或 UIOP : Interacting with background process

emacs - 实时可视化 S 表达式

clojure - 理解 Mini-Kanren 的执行模型

scheme - Andmap\ormap - chez 方案

recursion - 避免双重递归

java - 第 n 次调用函数

c# - 递归 HTTP 调用在 IDE 中表现出不同的行为与已部署的可执行文件