gpt4 book ai didi

list - 如何在 Haskell 中切换列表中的 2 个元素

转载 作者:行者123 更新时间:2023-12-01 16:39:13 24 4
gpt4 key购买 nike

我将从一个例子开始(我认为它会准确地说明我的问题)

switch 1 2 [[1,2,3,4],[5,6,0,7]] -> [[1,2,0,4],[5,6,3,7]]

其中 [[1,2,3,4],[5,6,0,7]] !! 1 ! 2 是零元素。第一个整数始终为 1,第二个整数介于 0 和 3 之间,我想通过他的索引(来自第二个组件列表)将我作为参数提供的元素更改为第一个组件列表中相同位置的元素。

我知道列表在 Haskell 中是不可变的,但我还是想不通。

我该怎么做?

最佳答案

switch _ n [xs,ys] = [xs',ys']
where (xs',ys',_) = unzip3 $
map (\t@(x,y,m) -> if m==n then (y,x,m) else t) $
zip3 xs ys [0..]

关于list - 如何在 Haskell 中切换列表中的 2 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30342740/

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