问题
How to access two dimensional array in prolog and how to loop each element in it For example if i have a matrix Question 1 how do i create a two-dimensional list like this in prolog:
1 2 3
4 5 6
7 8 9
Question 2: How do i loop each element and make every element +1 becomes
2 3 4
5 6 7
8 9 10
Question 3
public Cell(int row, int col, int cost, int units) {
this.cost = cost;
this.units = units;
this.row = row;
this.col = col;
}
In the matrix every thing is a object like cell My assignment is to transfer java code into Prolog code
So how can i do it make this object into prolog??
回答1:
Based on the way you are asking your questions, i.e. transfer java code into Prolog
it seems you think in procedural or object-oriented code and want to use the knowledge as leverage when learning Prolog. My advise is don't use your knowledge of procedural or object-oriented code as leverage or a fall back to help you learn Prolog because it will hinder your learning and effectiveness with Prolog. Instead try and embrace Prolog by learning to use it as it was intended.
Also when asking a question at Stackoverflow you should include the code of what you have tried.
How to access two dimensional array in Prolog?
So you want to create a data structure in Prolog that can work with data as two dimensional array. Without knowing further details a list of list should work, e.g.
[[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]
There are other possibilities but there is not enough detail in the question to suggest something more specific for a specific problem.
How do I loop each element and make every element +1.
First it is done using recursion so you can see what is happening in detail
increment(Array,New_array) :-
increment_rows(Array,New_array).
increment_rows([Row|Rows],[New_row|New_rows]) :-
increment_row(Row,New_row),
increment_rows(Rows,New_rows).
increment_rows([],[]).
increment_row([Cell|Columns],[New_cell|New_columns]) :-
increment_cell(Cell,New_cell),
increment_row(Columns,New_columns).
increment_row([],[]).
increment_cell(Cell,New_cell) :-
New_cell is Cell + 1.
?- increment([[[1],[2],[3]], [[4],[5],[6]], [[7],[8],[9]]], New_array).
New_array = [[2, 3, 4], [5, 6, 7], [8, 9, 10]].
now using the built-in predicate maplist/3
increment_item(Item,New_item) :-
New_item is Item + 1.
increment_list(List,New_list) :-
maplist(increment_item,List,New_list).
increment_array(Array,New_array) :-
maplist(increment_list,Array,New_array).
?- increment_array([[[1],[2],[3]], [[4],[5],[6]], [[7],[8],[9]]], New_array).
New_array = [[2, 3, 4], [5, 6, 7], [8, 9, 10]].
How can I make this object into Prolog?
Since standard Prolog is not Java and does not have objects, (some variations do though), I will toss out the word object and instead answer
How can I make this in Prolog?
This is one of the parts of Prolog that may seem very hard to grasp at first sight. In Prolog you do not declare variables to have types and you do not create constructor methods to create objects. In Prolog data is represented as terms and there are variables which are identified by starting with a capitol letter. So if you want something in Prolog which can hold values for row
, column
, cost
and units
you just use variables, e.g. Row
, Column
, Cost
and Units
, and can group them together in to a compound term, e.g. (Row,Column,Cost,Units)
, and if you want you can also qualify the structure with a name cell(Row,Column,Cost,Units)
.
来源:https://stackoverflow.com/questions/55094480/how-to-access-two-dimensional-array-in-prolog-and-how-to-loop-each-element-in-it