Search code examples
eclipse-clp

ECLiPSe CLP Type error in dim?


I'm trying to get the dimensions of a sudoku board in ECLiPSE CLP, however I get the following error:

type error in dim([[_389, 9, 8, _395, _397, _399, _401, _403, _405], [_409, _411, _413, _415, 7, _419, _421, _423, _425], [_429, _431, _433, _435, 1, 5, _441, _443, _445], [1, _451, _453, _455, _457, _459, _461, _463, _465], [_469, _471, _473, 2, _477, _479, _481, _483, 9], [_489, _491, _493, 9, _497, 6, _501, 8, 2], [_509, _511, _513, _515, _517, _519, _521, 3, _525], [5, _531, 1, _535, _537, _539, _541, _543, _545], [_549, _551, _553, 4, _557, _559, _561, 2, _565]], [_567, _569])

I get this when I call dim(Board,[R,C]), where Board is a sudoku board:

Board = [
[_, 9, 8, _, _, _, _, _, _],
[_, _, _, _, 7, _, _, _, _],
[_, _, _, _, 1, 5, _, _, _],
[1, _, _, _, _, _, _, _, _],
[_, _, _, 2, _, _, _, _, 9],
[_, _, _, 9, _, 6, _, 8, 2],
[_, _, _, _, _, _, _, 3, _],
[5, _, 1, _, _, _, _, _, _],
[_, _, _, 4, _, _, _, 2, _]].

Anyone any idea why this happens?


Solution

  • You are calling dim/2 which expects an array as first argument. However Board in your case, is a list. A simple conversion can be obtained by calling:

    array_list(BoardArray,Board)
    

    Small note: since a sudoku will always have an equal amount of rows and columns, you could also use the same variable for stating the dimension. Like so:

    dim(Sudoku,[N,N])
    

    EDIT:

    In order to also convert the inner lists, you want to iterate through the list of lists and convert every row list into an array. This goes like following:

    (foreach(Row,Board), foreach(RowArray,Out)
    do
      array_list(RowArray,Row)
    ),
    array_list(BoardArray,Out)
    

    We iterate simultaneously through Board and Out in order to convert every row-list into a row-array and after the loop, we also convert the parent board list to a board array.

    This appeared to be already answered here .