gpt4 book ai didi

lisp - 如何在 chicken scheme 中打印 defstruct 生成的数据结构的所有 "parts"

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:16 24 4
gpt4 key购买 nike

假设我们有上面的代码:

(require-extension defstruct)

(defstruct tree height age leaf-color)

(define coconut
(make-tree height:30
age: 5
leaf-color: 'green))

我知道我可以使用 (tree-height coconut) 来查看椰子的高度,但是如何在一个命令中查看椰子的所有信息呢?我也试过 (tree->alist coconut) 产生: ((height . 30) (age . 5) (leaf-color . 'green)) 但我不能使用类似的东西:(for-each pp coconut)。是否可以这样做,或者使用适当的命令编写我的打印树是唯一的解决方案?

最佳答案

所以这不是一个通用的解决方案,因为它使用了特定的 tree->alist 过程。此外,输出不是特别漂亮。正如我在评论中提到的,如果您有非常具体的打印需求,您应该查看 format egg。

(use defstruct)

(defstruct tree height age leaf-color)

(define coconut (make-tree height: 30 age: 5 leaf-color: 'green))

(define (pp-tree t)
(let loop ((attr (tree->alist t)))
(cond ((null? attr) 'done)
(else
(display (caar attr))(display ": ")
(display (cdar attr))(newline)
(loop (cdr attr))))))

关于lisp - 如何在 chicken scheme 中打印 defstruct 生成的数据结构的所有 "parts",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10466934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com