gpt4 book ai didi

arrays - Haskell:如何结合标量和单值?

转载 作者:行者123 更新时间:2023-12-02 08:40:51 25 4
gpt4 key购买 nike

目前我正在摆弄有关 2D ST 数组和递归的 haskell 问题。

给定一个 2D 位置和一个方向数组,我写了一行返回数组内所有结果点的列表:

let cellsAround = [resPt | dir <- directions,
let resPt = (fst dir + fst point, snd dir + snd point),
fst resPt >= 0 && fst resPt <= fst maxIdx &&
snd resPt >= 0 && snd resPt <= snd maxIdx]

现在的目标是用数组的内容丰富结果列表项,我尝试了这个:

cellsAround <- sequence [readArray board resPt | dir <- directions,
let resPt = (fst dir + fst point, snd dir + snd point),
fst resPt >= 0 && fst resPt <= fst maxIdx &&
snd resPt >= 0 && snd resPt <= snd maxIdx]

这也很有效。但目标是获得 [(Point, Int)] 的组合,因为我必须过滤数组内容。

任何想法如何将其结合起来,比如说

(resPt, readArray board resPt)

最佳答案

最小的改变:

sequence [(,) resPt <$> readArray board resPt
| ...

关于arrays - Haskell:如何结合标量和单值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34064797/

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