如何在序言中访问二维数组以及如何循环其中的每个元素 [英] How to access two dimensional array in prolog and how to loop each element in it

查看:72
本文介绍了如何在序言中访问二维数组以及如何循环其中的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在序言中访问二维数组以及如何循环其中的每个元素例如,如果我有一个矩阵问题1如何在序言中创建像这样的二维列表:

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

问题2:我如何循环每个元素并使每个元素+1变成

Question 2: How do i loop each element and make every element +1 becomes

  2 3 4
  5 6 7
  8 9 10

问题3

 public Cell(int row, int col, int cost, int units) {
        this.cost = cost;
        this.units = units;
        this.row = row;
        this.col = col;
    }

在矩阵中,所有事物都是像单元格一样的对象我的任务是将Java代码转换为Prolog代码

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??

推荐答案

根据您提出问题的方式,即将Java代码转移到Prolog 中,您似乎认为是面向过程的还是面向对象的代码,并希望在学习Prolog时将这些知识用作杠杆.我的建议是不要将您对过程或面向对象的代码的知识用作杠杆或后备帮助您学习Prolog,因为这会妨碍您使用Prolog的学习和效率.而是通过学习按预期使用Prolog来尝试拥抱Prolog.

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.

另外,当在Stackoverflow上提问时,您还应该包括您尝试过的代码.

Also when asking a question at Stackoverflow you should include the code of what you have tried.

如何在Prolog中访问二维数组?

How to access two dimensional array in Prolog?

因此,您要在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.

如何循环每个元素并使每个元素+1.

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

现在使用内置谓词 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]].

如何使该对象成为Prolog?

How can I make this object into Prolog?

由于标准的Prolog不是Java且没有对象(尽管有些变化),所以我将抛出对象一词,而是回答

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

如何在Prolog中做到这一点?

How can I make this in Prolog?

这是Prolog的一部分,乍一看似乎很难理解.在Prolog中,您不会声明变量具有类型,也不会创建构造函数方法来创建对象.在Prolog中,数据表示为术语,并且存在以大写字母开头的变量.因此,如果您希望在Prolog中使用某些东西来保存 row column cost units 的值,则只需使用变量,例如 Row Column Cost Units ,并且可以将它们组合到一个复合词中,例如(Row,Column,Cost,Units),如果需要,您还可以使用名称 cell(Row,Column,Cost,Units)来限定结构.

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).

这篇关于如何在序言中访问二维数组以及如何循环其中的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆