代码之家  ›  专栏  ›  技术社区  ›  user8866063

在递归过程中打印

  •  0
  • user8866063  · 技术社区  · 6 年前

    我有一个以面向对象编程范式设计的堆栈过程,堆栈中的一个嵌套过程是print。由于我的堆栈实际上是一个列表,我可以通过调用全局变量my stack来轻松地打印它,但我不想将其打印为列表,我想将其打印为一个堆栈,一个在另一个之上,有方法吗?

    (define (print)
    (define (print-helper stack)
      (if (empty?)'()
          (print-helper (cdr stack))
    ))
    (print-helper my-stack))
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Óscar López    6 年前

    如果它是一个堆栈,我们希望打印首先添加的最后一个元素。这应该可以做到:

    (define (print)
      (define (print-helper stack)
        (cond ((empty? stack) 'done)
              (else
               (print-helper (cdr stack))
               (display (car stack))
               (newline))))
      (print-helper my-stack))