prolog

How to access two dimensional array in prolog and how to loop each element in it

隐身守侯 提交于 2021-02-09 18:54:56
问题 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

How to access two dimensional array in prolog and how to loop each element in it

故事扮演 提交于 2021-02-09 18:50:48
问题 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

Goal expansion for an `if_/3` operator in Prolog

帅比萌擦擦* 提交于 2021-02-09 11:20:08
问题 I'm writing a tokeniser and I want to use if_/3 to preserve logical-purity in my code. The code looks like the following code 1 on the left—but I want it to look like the one on the right. if_(Cond1_1, % ( Cond1_1 Then1, % *=> Then1 if_(Cond2_1, % ; Cond2_1 Then2, % *=> Then2 if_(Cond3_1, % ; Cond3_1 Then3, % *=> Then3 if_(Cond4_1, % ; Cond4_1 Then4, % *=> Then4 if_(Cond5_1, % ; Cond5_1 Then5, % *=> Then5 Else5 % ; Else5 ) ) ) ) ). % ). To do the rewriting of (*=>)/2 to if_/3 in SWI-Prolog I

How to enable the occurs check in all unifications in SWI-Prolog?

只愿长相守 提交于 2021-02-09 04:14:37
问题 According to Wikipedia: Implementations offering sound unification for all unifications are Qu-Prolog and Strawberry Prolog and (optionally, via a runtime flag): XSB, SWI-Prolog and Tau Prolog. However, when I do apropos(occur) it only finds unify_with_occurs_check/2 . The man page doesn't mention "occur" either. How can the occurs check be enabled for all unifications in SWI-Prolog? 回答1: In the section on Environment Control, it lists the flags of the system. The occurs_check flag is the one

Prolog: Find even numbers add them on a list

陌路散爱 提交于 2021-02-08 11:59:32
问题 Write predicate evenNumbers(L1, L2) which is true if the list L1 containing random integers and the list L2 contains even integers from L1 . For example: ?-evenNumbers ([2,1,-3,6,8,9], L2). »Your program returns L2 = [2,6,8]. My code is: evenNumbers([],[]). evenNumbers([H|T],L):- integer(H), 0 is H mod 2, append([H],L,L); evenNumbers(T,L). 回答1: Your code has multiple issues append([H],L,L); will stop recursion and give you a wrong list also your if-then-else statement isn't right .So you

A*(Star) Prolog

筅森魡賤 提交于 2021-02-08 05:57:38
问题 I have an A*(Star) Algorithm in prolog, and I need receive a list of destinations instead a single destination, and it should past for every element of the list and then back to the start. NOTE: It can pass in the same place twice. I tried it, but Swi-prolog returns false everytime, can I get any help? The code above receive a single destination, and it works, but as I said I need a list of destinations, and pass through them all. Example: astar(1,[2,2,4,6,8,9,10,13,15],C,P). /*area(Number

A*(Star) Prolog

泪湿孤枕 提交于 2021-02-08 05:56:19
问题 I have an A*(Star) Algorithm in prolog, and I need receive a list of destinations instead a single destination, and it should past for every element of the list and then back to the start. NOTE: It can pass in the same place twice. I tried it, but Swi-prolog returns false everytime, can I get any help? The code above receive a single destination, and it works, but as I said I need a list of destinations, and pass through them all. Example: astar(1,[2,2,4,6,8,9,10,13,15],C,P). /*area(Number

Prolog infix operator definition

﹥>﹥吖頭↗ 提交于 2021-02-07 20:37:42
问题 I am recently learning about Prolog and I find the three types used for defining infix operators confusing. What are the differences between xfx, xfy and yfx when specifying the type of an operator? I have googled about the problem and haven't found anything useful. I tried typing the following codes in Prolog: :- op(500,yfx,is_alive). is_alive(A,B) :- display([A,B]). :- op(500,xfy,is_alive2). is_alive2(A,B) :- display([A,B]). :- op(500,xfx,is_alive3). is_alive3(A,B) :- display([A,B]). and

Prolog infix operator definition

旧时模样 提交于 2021-02-07 20:32:12
问题 I am recently learning about Prolog and I find the three types used for defining infix operators confusing. What are the differences between xfx, xfy and yfx when specifying the type of an operator? I have googled about the problem and haven't found anything useful. I tried typing the following codes in Prolog: :- op(500,yfx,is_alive). is_alive(A,B) :- display([A,B]). :- op(500,xfy,is_alive2). is_alive2(A,B) :- display([A,B]). :- op(500,xfx,is_alive3). is_alive3(A,B) :- display([A,B]). and

Subtracting or adding lists of lists in prolog?

点点圈 提交于 2021-02-07 18:36:20
问题 I am fairly new to prolog and am trying to mess around with lists of lists. I am curious on how to add two lists of lists or subtract them resulting in one list of list. If I have two lists of lists lets say, SomeList = [[1,2,3,4],[5,6,7,8]] SomeList2 = [[1,2,3,4],[5,6,7,8]] How could I add or subtract SomeList and SomeList2 to create a list of lists? Resulting in a sum of say sumList([[2,4,6,8],[10,12,14,16]]) or vice-versa for subtraction? Any help would be appreciated not looking for code