我正在使用此代码来确保将列表放在一起,以便将其与我正在编写的另一个函数一起使用。
#lang racket
(define path_from_start null)
(define path_stack null)
(define input1 '(0 0))
(define input2 '(1 2))
(define input3 '(3 1))
(define (test xy)
(set! path_from_start (append path_from_start (list xy)))
(display "path from start:") (display path_from_start)
(display "\n")
(set! path_stack (list path_from_start path_stack))
(display "path_stack")(display path_stack)
(display "\n")
;(set! path_stack (cdr path_stack))
;(display path_stack)
;(display "\n")
)
(test input1)
(test input2)
(test input3)
但是我得到以下输出:
path from start:((0 0))
path_stack(((0 0)) ())
path from start:((0 0) (1 2))
path_stack(((0 0) (1 2)) (((0 0)) ()))
path from start:((0 0) (1 2) (3 1))
path_stack(((0 0) (1 2) (3 1)) (((0 0) (1 2)) (((0 0)) ())))
1st item on path_stack: '((0 0) (1 2) (3 1))
我不希望在path_stack的末尾有空列表。它不会显示在path_from_start中。这可能是因为我形成path_stack的方式。但我想保留我现在使用的方括号格式。我可以将path_stack初始化为另一个变量吗?
我查找了其他方法,但它们都破坏了括号格式。我是否应该在第一步中使用其中一种方法?只需在列表中添加其他1个元素即可将其压缩在萌芽状态,这样我就可以轻松地对其进行修改?
谢谢。
(set! path_stack (list* path_from_start path_stack))
结果是,我相信这可以解决括号中的问题-问题中的示例输出似乎不正确:
path from start: ((0 0))
path_stack: (((0 0)))
path from start: ((0 0) (1 2))
path_stack: (((0 0) (1 2)) ((0 0)))
path from start: ((0 0) (1 2) (3 1))
path_stack: (((0 0) (1 2) (3 1)) ((0 0) (1 2)) ((0 0)))
现在,我们可以追溯路径中的每个步骤,如下所示:
(first path_stack)
=> '((0 0) (1 2) (3 1))
(second path_stack)
=> '((0 0) (1 2))
(third path_stack)
=> '((0 0))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句