简体   繁体   English

如何通过应用于它们各自的第一个和第二个值的谓词过滤具有 (Int, Int) 值的元组列表?

[英]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.

相关问题 如何将 int 的值与元组列表中的同一组组合? - How to combine values of int's with the same group in list of tuples? 程序输出不符合预期-在元组列表中搜索int值 - Program output is not as expected - Searching for int values in a list of tuples 基于_._ 1的样式(String,Int)的元组列表的Scala求和值 - Scala Summing Values of a list of Tuples in style (String,Int) based on _._1 使用与 int 值对应的元组列表,想要创建与所有 int 值的总和对应的那些元组的唯一列表(python) - With list of tuples corresponding to int values, want to create a unique list of those tuples corresponding to the sum of all the int values (python) 比较列表(int值)与1 int - compare a list (int values) with 1 int 如何从元组中获取第二个值与下一个元组中的第一个值相同的值列表? - How to get a list of values from tuples which the second value is the same as first in the next tuple? 使用与 int 值列表相对应的元组列表,创建与列表中每个值的总和相对应的列表(python) - With list of tuples corresponding to a list of int values, create list corresponding to sum of each value in the list (python) Scala过滤器列表[Int]存在于其他元组列表中 - Scala Filter List[Int] Which Exists in other List of Tuples 将列表中的某些值转换为int - Convert certain values in list to int Python列表中的元组列表 - Python list of tuples to list of int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM