gpt4 book ai didi

struct - Racket 契约(Contract)和结构问题

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

我正在为人工智能类(class)学习 Racket。对于第一个项目,老师给了我们一个包含契约(Contract)和单元测试的文件,我们要编写使其运行所需的功能。我刚刚创建了我需要的功能的 stub ,并满足了除一个之外的所有契约(Contract):

[start-state (and/c state? (not/c state-game-over?))]

我声明的函数现在看起来像这样:
(define (start-state)
(state '() start-tiles 0)
)

状态结构是老师给出的:
(struct state (played unplayed passes) #:prefab)

有契约(Contract):
[struct state ((played (listof (and/c tile? tile-at-origin?)))
(unplayed (listof (and/c tile? tile-on-board?)))
(passes pass-count?))]

这会因错误而崩溃:
start-state: broke its contract
promised: (and/c state? (not/c state-game-over?))
produced: #<procedure:start-state>
which isn't: state?
in: (and/c state? (not/c state-game-over?))
contract from:

我相信我的起始状态过程创建并返回一个状态结构,但显然它返回自身并违反了契约(Contract)。如何返回结构而不是过程?

最佳答案

它看起来像 start-state不应该是一个过程,而是一个值。也就是说,你需要做

(define start-state (start ...))

而不是
(define (start-state) ...)

关于struct - Racket 契约(Contract)和结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19262123/

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