gpt4 book ai didi

NetLogo:以紧凑的方式对列表中的一项执行操作的过程?

转载 作者:行者123 更新时间:2023-12-02 10:29:32 26 4
gpt4 key购买 nike

NetLogo 新手...想知道是否有一个过程可以以紧凑的方式对列表中的一项执行操作(类似于 map ,但针对一项)。

例如,假设我想将 3 添加到列表 blah 中索引 i 处的项目。

现在我正在这样做:

设置 blah 替换项目 i blah(项目 i blah + 3)

这似乎有点笨重,好像会有一个程序可以做到这一点,但我一直找不到一个。只是想确保我没有遗漏什么。

谢谢!泰勒

最佳答案

没有内置的东西可以做到这一点。但您可以自己将其定义为将任务作为输入的过程:

;; replace item i of xs with the result of applying fn to that item
to-report mapping-replace-item [i xs fn]
report replace-item i xs (runresult fn item i xs)
end

使用示例:

observer> show mapping-replace-item 2 [10 20 30 40] task [? * ?]
observer: [10 20 900 40]

关于NetLogo:以紧凑的方式对列表中的一项执行操作的过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21354589/

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