Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何递归遍历Haskell中的3元组列表_Haskell_Recursion_Functional Programming - Fatal编程技术网

如何递归遍历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

提示:。