方案重新定义列表
我有一个称为手的列表,另一个称为牌组,这里的主要目标是在我调用功能抽牌时取出列表牌组中的第一张牌(或元素)并将其放入列表手中......
> (draw hand deck)
(2 C)
> (draw hand deck)
(2 C) (3 H)
> (draw hand deck)
(2 C) (3 H) (K D)
但每次我调用它的手永远不会改变价值...... 我不知道有没有像 O-Object 那样的方法来永久改变手的内容?
我最初定义手牌为空,因为玩家没有牌可以开始。
(define hand '())
I have a list called hand and another one called deck, the main goal here is to take the first card (or element ) in the list deck and put it in the list hand when i call the fnction draw...
> (draw hand deck)
(2 C)
> (draw hand deck)
(2 C) (3 H)
> (draw hand deck)
(2 C) (3 H) (K D)
but everytime i call it the hand never changes value...
I'm clueless is there a way like in O-Object to change the content of hand permenantly?
and i initialy define hand empty because the player has no card to start.
(define hand '())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个功能性的解决方案,
draw
没有副作用:A functional solution, with
draw
being side-effect free:您无法更改列表的内容,但可以更改名称引用的列表。所以:
现在每个列表 '("post") '("first" "post") 和 '(一个全新的列表) 都是不可更改的(“不可变”)列表,但名称 some-list first 指向一个,然后是另一个,然后是第三个。
警告:对于许多问题,您需要避免设置!并尝试以不同的方式思考问题。例如,如果您在游戏中使用世界和宇宙,
http://pre.plt-scheme.org/plt/doc/ teachingpack/2htdpuniverse.html
那么你会希望你的更新程序返回一个新的世界状态而不是使用 set!修改旧的。
You cannot change the contents of a list, but you can change which list a name refers to. So:
Now each of the lists '("post") '("first" "post") and '(a completely new list) are unchangeable ("immutable") lists, but the name some-list first points to one, then another, then the third.
Caveat: For many problems, you will want to avoid set! and try to think about the problem a different way. For example, if you work with worlds and universes for your game,
http://pre.plt-scheme.org/plt/doc/teachpack/2htdpuniverse.html
then you'll want your updaters to return a new state of the world rather than using set! to modify the old one.
哦,接下来你会发现,从调用该函数的人的角度来看,更改函数内名称所指的列表不会改变该名称所指的内容。所以:
Oh, and next you will find that changing what list a name refers to inside a function will not change what the name refers to from the perspective of whoever called the function. So:
Vijay 为Scheme 提供了最佳解决方案。但是,如果您确实想要通过永久更改列表来实现此目的,则需要使用
set-car!
和set-cdr!
代码>.这在Scheme中并不自然,需要一些技巧才能使其工作:首先定义
hand
和deck
:hand
必须以现有元素,以便它有一些现有的列表结构需要修改。您不能将set-car!
和set-cdr!
与 nil ('()
) 一起使用。现在写
draw
:这意味着你手上的最后一个元素将始终是虚拟的。最好添加对初始情况的检查并覆盖它而不是推送:
此外,您应该在执行任何操作之前检查
from
是否为空。Vijay has the best solution for Scheme. However, if you really want to make this work by changing the lists permanently, you'll need to use
set-car!
andset-cdr!
. This is not natural in Scheme, and requires a couple of hacks to make it work:First define
hand
anddeck
:hand
has to start with an existing element so that it has some existing list structure to modify. You can't useset-car!
andset-cdr!
with nil ('()
).Now write
draw
:This means the last element of your hand will always be dummy. It would be better to add a check for the initial case and overwrite it instead of pushing:
Also you should check that
from
isn't empty before doing anything.