[英]How to filter a list of tuples with (Int, Int) values by a predicate applied to their respective first and second values?
Suppose i have a list of tuples representing positions on a grid by their x and y values.假设我有一个元组列表,通过它们的 x 和 y 值表示网格上的位置。
The tuples are defined as type Pos, a pair of Integer values.元组被定义为 Pos 类型,即一对整数值。
The Board is further defined as a list of Pos. Board 被进一步定义为 Pos 列表。
type Pos = (Int, Int)
type Board = [Pos]
exampleBoard :: Board
exampleBoard = [(1, 1), (1, 2), (2, 2), (2, 3), (1, 3),
(5, 1), (5, 2), (4, 2), (4, 1), (5, 3),
(9, 10), (9, 11), (9, 12), (9, 13),
(10, 10), (10, 11), (10, 12), (10, 13),
(10, 20), (11, 20), (12, 20), (13, 20)]
The Board is ax*x grid, where you can always consider the known variables height and width having the same value. Board 是 ax*x 网格,您可以在其中始终考虑具有相同值的已知变量高度和宽度。 height = width = size高度 = 宽度 = 大小
If the x or y value is not in a certain range (0<x<size || 0<y<size) I want the tuple removed.如果 x 或 y 值不在某个范围内 (0<x<size || 0<y<size) 我想删除元组。
Is there a simple way to filter the list as I describe?有没有一种简单的方法来过滤我所描述的列表? From searching for similar questions I have tried using library functions such as "break" and "span" to no success as of yet.从搜索类似的问题,我尝试使用诸如“break”和“span”之类的库函数,但到目前为止还没有成功。
To test if a tuple (Int,Int)
is in a given range, you can use the inRange
function:要测试元组(Int,Int)
是否在给定范围内,您可以使用inRange
函数:
import Data.Ix (inRange)
inRange ((1,1),(10,10)) (5,5) -- True
inRange ((1,1),(10,10)) (11,6) -- False
filter (inRange ((1,1),(10,10))) [(5,5),(11,6)] -- [(5,5)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.