-2

I am quite new at haskell. Our task is to write an algorithm that calculates the dimension of a square matrix. But we are not supposed to use lists. But I dont know how to access to each element of the matrix, which is given like that:

matrixA 1 1 = 0
matrixA 1 2 = 42
matrixA 1 3 = 1337
matrixA 2 1 = 501
matrixA 2 2 = 314
matrixA 2 3 = 301
matrixA 3 1 = 13
matrixA 3 2 = 161
matrixA 3 3 = 271
matrixA _ _ = -1
Memphis
  • 99
  • 1
  • 1
  • 4

1 Answers1

2

To access the element of the matrix at row i and column j, you can simply use matrixA i j. In fact, matrixA is a function which maps both indexes to the matrix element.

I guess that the last -1 case represents an invalid value, meaning "indexes outside the matrix". So, it seems that to find the size you just have to query the matrix with larger and larger row/column indexes until you get -1. A basic way to solve that is to proceed recursively.

chi
  • 111,837
  • 3
  • 133
  • 218