在递归过程中打印

printing in a recursive procedure

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

(define (print)
(define (print-helper stack)
  (if (empty?)'()
      (print-helper (cdr stack))
))
(print-helper my-stack))

如果是栈,我们要先打印最后添加的元素。这应该可以解决问题:

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