如何递归遍历Haskell中的3元组列表
我有一个三元组的列表如何递归遍历Haskell中的3元组列表,haskell,recursion,functional-programming,Haskell,Recursion,Functional Programming,我有一个三元组的列表[(Int,Int,Int)] 我已经编写了以下助手函数 --This function is used to check if the first element is over 20 checkValue :: (Int, Int, Int) -> Bool checkValue (x, _, _) = x > 20 --This function is used to set the 3-tuple to return (50, 50, 5
[(Int,Int,Int)]
我已经编写了以下助手函数
--This function is used to check if the first element is over 20
checkValue :: (Int, Int, Int) -> Bool
checkValue (x, _, _) = x > 20
--This function is used to set the 3-tuple to return (50, 50, 50)
setValue :: (Int, Int, Int) -> (Int, Int, Int)
setValue a = (50, 50, 50)
我的目标是遍历3元组列表并应用我的helper函数
--This function is used to check if the first element is over 20
checkValue :: (Int, Int, Int) -> Bool
checkValue (x, _, _) = x > 20
--This function is used to set the 3-tuple to return (50, 50, 50)
setValue :: (Int, Int, Int) -> (Int, Int, Int)
setValue a = (50, 50, 50)
对于列表中的每个项目
- 运行checkValue
- 如果checkValue=true,则将setValue应用于当前元组
- 继续
[(0,0,0)(30,15,0)]
它将返回[(0,0,0)(50,50,50)]
有人能给我指出正确的方向吗?我已经在这上面停留了一段时间。如果你想使用递归,那么你可以做如下操作
modif :: [(Int, Int, Int)] -> [(Int, Int, Int)]
modif [] = []
modif (t@(x, _, _) : ts) = case x > 20 of
True -> (50, 50, 50) : modif ts
_ -> t : modif ts
*Main> modif [(0, 0, 0),(30,15,0)]
[(0,0,0),(50,50,50)]
t@(x,_,_)
部分代表元组第一个元素上的模式加工x
,也将整个元素命名为t
提示:。