gpt4 book ai didi

scheme - 避免一个结构体显示 3 次

转载 作者:行者123 更新时间:2023-12-04 10:49:04 25 4
gpt4 key购买 nike

我定义了一个结构如下,

(struct vector (x y z)
#:methods gen:custom-write
[(define (write-proc vector port mode)
(let ([print (if mode write display)])
(write-string "<")
(print (vector-x vector))
(write-string ", ")
(print (vector-y vector))
(write-string ", ")
(print (vector-z vector))
(write-string ">")))])

但是我在 REPL 中出现了一个奇怪的行为,其中结构被显示了 3 次:
> (define a (vector 1 2 3))
> a
<1, 2, 3><1, 2, 3><1, 2, 3>

我一定是做错了什么,但找不到我的问题。有人能解释一下为什么我有 3 倍的输出吗?

最佳答案

将输出定向到输出端口,一切正常:

#lang racket
(struct vector (x y z)
#:methods gen:custom-write
[(define (write-proc vector port mode)
(let ([print (if mode write display)])
(write-string "<" port)
(print (vector-x vector) port)
(write-string ", " port)
(print (vector-y vector) port)
(write-string ", " port)
(print (vector-z vector) port)
(write-string ">" port)))])

关于scheme - 避免一个结构体显示 3 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32355791/

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