首页 > 解决方案 > 循环遍历二维数组和记录位置的 Haskell 等价性

问题描述

我如何在haskell中编写这个java代码。我想遍历一个二维数组来检查一个人(机器)和工作对并记录机器的位置。

int  col = 0;   
int row = 0;
int count = 0;  // accumulator

// a loop to check 2D array for m and t pairs
for(int i = 0; i < input.length; i++) {
   for(int j = 0; j < m.length; j++) {
      if(input[i].charAt(0) == m[j]) {
      col = j;    // recording position of m for 2D array
      break;
      }
}
for(int j = 0; j < t.length; j++) {
   if(input[i].charAt(1) == t[j]) {
     row = j;
     break;
     }
}

标签: haskell

解决方案


您可以使用findIndex来查找列表中与谓词匹配的第一个元素的索引(如果存在)。


推荐阅读