Haskell 如何将iset与嵌套数据类型和镜头一起使用?

Haskell 如何将iset与嵌套数据类型和镜头一起使用?,haskell,indices,haskell-lens,custom-data-type,Haskell,Indices,Haskell Lens,Custom Data Type,我无法使最后一个函数中的类型对齐。重点是设置与只依赖于3元组索引的函数连接的所有价格加倍。元组中的原始双精度值可以丢弃 {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TupleSections #-} import Control.Lens data Typex = Typex { _level :: Int , _coordinate :: (Int, Int) , _connections :: [

我无法使最后一个函数中的类型对齐。重点是设置与只依赖于3元组索引的函数连接的所有价格加倍。元组中的原始双精度值可以丢弃

{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TupleSections #-}

import Control.Lens

data Typex = Typex 
    { _level       :: Int
    , _coordinate  :: (Int, Int)
    , _connections :: [(Int, (Int, Int), Double)]  -- = (level, coordinate, price)
    } deriving Show
makeLenses ''Typex

initTypexLevel :: Int -> Int -> Int -> [Typex] 
initTypexLevel a b c = [ Typex a (x, y) [(0,(0,0),0.0)]
                       | x <- [0..b], y <- [0..c]
                       ]

buildNestedTypexs :: [(Int, Int)] -> [[Typex]]
buildNestedTypexs pts
     = setConnectionsx [ initTypexLevel i y y
                      | (i,(_,y)) <- zip [0..] pts
                      ]

setConnectionsx :: [[Typex]] -> [[Typex]]
setConnectionsx (x:rest@(y:_)) = map (connect y) x : setConnectionsx rest
  where connect :: [Typex] -> Typex -> Typex
        connect txs tx
          = tx & connections .~ (map ((tx ^. level) + 1, , 0.0) $ txs ^.. traverse.coordinate)
setConnectionsx lst = lst

setInitPrices  :: [[Typex]] -> [[Typex]]
setInitPrices  (x:rest) = map setIndexPrices x : setInitPrices  rest
  where setIndexPrices :: Typex -> Typex
        setIndexPrices tx =  n & connections .~ ??? -- using iset (?), set the price in every 3-tuple so that price = f (index of the 3-tuple) where f = i*2
setInitPrices  lst = lst
{-#语言模板haskell}
{-#语言元组{-}
进口管制.镜头
数据类型x=类型x
{u级别::Int
,_坐标::(Int,Int)
,_连接::[(Int,(Int,Int,Double)]-->=(级别,坐标,价格)
}衍生节目
makeX
initTypexLevel::Int->Int->Int->[Typex]
inittypexlevelabc=[typexa(x,y)[(0,(0,0),0.0]
|x类型->类型x
连接txs tx
=tx&connections.~(map((tx^.level)+1,0.0)$txs^..遍历.坐标)
setConnectionsx lst=lst
setInitPrices::[[Typex]]->[[Typex]]
setInitPrices(x:rest)=映射setIndexPrices x:setInitPrices rest
其中setIndexPrices::Typex->Typex
setIndexPrices tx=n&connections.~??——使用iset(?)设置每个三元组中的价格,使价格=f(三元组的索引),其中f=i*2
setInitPrices lst=lst

您可能正在寻找:

  where setIndexPrices :: Typex -> Typex
        setIndexPrices tx =  tx & connections .> traversed <. _3 .@~ f
        f i = 2 * fromIntegral i
此光纤采用
TypeX
,关注其
\u连接
字段,遍历连接列表,并关注每个连接的第三个字段(价格)。结果是光纤按顺序遍历
TypeX
中的所有价格

为了索引此光学元件,我们需要将未索引的
遍历
升级为索引的
遍历
。然后,我们要使用保留索引的合成运算符
[[Typex]]

setInitPrices'=traverse.traverse.connections.>完全符合我的需要。谢谢您的详细解释。这个问题中的示例工作得很完美,但在我自己的代码中使用它会产生以下错误:
*由于使用
traverse'而产生的不明确类型变量
t0'会阻止约束t
(可遍历的t0)”的问题。相关绑定包括setInitPrices'::t0(t1 MyType)->t0(t1 MyType)可能的修复方法:使用类型批注指定
t0'应该是什么
函数类型已明确说明,因此这意味着什么批注?此消息声称
setinitPrices'
没有类型签名。您是否可能在类型签名和定义中拼写函数名称不同?
connections . traverse . _3
connections .> traversed <. _3
setInitPrices' :: [[Typex]] -> [[Typex]]
setInitPrices' = traverse .> traverse .> connections .> traversed <. _3 .@~ f
  where f i = 2 * fromIntegral i
a .> b .> c .> d <. e <. f <. g
a . b . c .> d <. e . f . g
setInitPrices' :: [[Typex]] -> [[Typex]]
setInitPrices' = traverse . traverse . connections .> traversed <. _3 .@~ f
  where f i = 2 * fromIntegral i