prolog

Prolog lists of ascending number inside a list

落爺英雄遲暮 提交于 2020-01-15 12:15:21
问题 I Have a list of [1,2,3,1,0] at start but need to split it into a number of sub lists where the new lists becomes [[1,2,3],[1],[0]]. The basic concept that I know in prolog is by comparing numbers. ascending([Head | [HeadTail|TailTail]]) :- Head =< HeadTail. 回答1: we can do with basic list' pattern matching ascending([A], [[A]]). ascending([A,B|T], R) :- ( A > B -> R = [[A],P|Q] ; P = [M|N], R = [[A,M|N]|Q] ), ascending([B|T], [P|Q]). test: 1 ?- ascending([1,2],X). X = [[1, 2]] ; false. 2 ?-

Finding the Palindrome of a List using Prolog :- Exercise 3.5 “ Prolog Programming for Artificial Intelligence” by Ivan Btrako

杀马特。学长 韩版系。学妹 提交于 2020-01-15 11:23:49
问题 I've been programming with java for the past 3 years, and I have just started learning Prolog. I downloaded SWI-Prolog to run my code on and trace, but I'm having trouble with this particular question. /*concatinates 2 Lists together*/ conc([],L,L). conc([X|T],L2,[X|T1]):- conc(T,L2,T1). /*Finds the reverse a List*/ reverse([],[]). reverse([H|T],ReversedList):- reverse(T,Reverse), conc(Reverse,[H],ReversedList). palindrome(L):- reverse(L,L1), L is L1. for example if I enter palindrome([m,a,d

Finding the Palindrome of a List using Prolog :- Exercise 3.5 “ Prolog Programming for Artificial Intelligence” by Ivan Btrako

你说的曾经没有我的故事 提交于 2020-01-15 11:23:16
问题 I've been programming with java for the past 3 years, and I have just started learning Prolog. I downloaded SWI-Prolog to run my code on and trace, but I'm having trouble with this particular question. /*concatinates 2 Lists together*/ conc([],L,L). conc([X|T],L2,[X|T1]):- conc(T,L2,T1). /*Finds the reverse a List*/ reverse([],[]). reverse([H|T],ReversedList):- reverse(T,Reverse), conc(Reverse,[H],ReversedList). palindrome(L):- reverse(L,L1), L is L1. for example if I enter palindrome([m,a,d

Prolog: How can I implement the sum of squares of two largest numbers out of three?

半腔热情 提交于 2020-01-15 06:18:32
问题 Exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. I'm learning Prolog. Here's the function I tried to implement: square(X, Y) :- Y is X * X. squareTwoLargest(X, Y, Z, R) :- R is square(L1) + square(L2), L1 = max(X, Y), L2 = max(min(X, Y), Z). However, when I run it, it gives the following error: ERROR: is/2: Arguments are not

Prolog: How can I implement the sum of squares of two largest numbers out of three?

痞子三分冷 提交于 2020-01-15 06:17:08
问题 Exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. I'm learning Prolog. Here's the function I tried to implement: square(X, Y) :- Y is X * X. squareTwoLargest(X, Y, Z, R) :- R is square(L1) + square(L2), L1 = max(X, Y), L2 = max(min(X, Y), Z). However, when I run it, it gives the following error: ERROR: is/2: Arguments are not

Prolog - Calculating coordinates of Koch-curve

半城伤御伤魂 提交于 2020-01-15 06:16:26
问题 I'm learning about constraint programming and recursive programming in Prolog. I have to programm a koch-curve of Level N which should start at (Sx,Sy) and end at (Ex,Ey) .The line-segments, which are being calculated, will be stored in Ls. When I try to execute generatelines(1,(60,0),(-60,0),Ls) , I get the right 4 coordinates of the koch-curve of level 1: [[ (60, 0), (20, 0)], [ (20, 0), (0.0, -34.64)], [ (0.0, -34.64), (-20, 0)], [ (-20, 0), (-60, 0)]] When I try to execute generatelines(2

Combinatios in List of LIsts Prolog

筅森魡賤 提交于 2020-01-15 04:00:12
问题 I need to find the combinations in a list of lists. For example give the following list, List = [[1, 2], [1, 2, 3]] These should be the output, Comb = [[1,1],[1,2],[1,3],[2,1],[2,2],[2,3]] Another example: List = [[1,2],[1,2],[1,2,3]] Comb = [[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3]....etc] I know how to do it for a list with two sublists but it needs to work for any number of sublists. I'm new to prolog, please help. 回答1: try([],[]). try([L|Ls],[M|Ms]):- member(M,L), try(Ls,Ms). all(L

Check if the position if less then or greater then another position prolog

别等时光非礼了梦想. 提交于 2020-01-15 03:35:13
问题 I have revised this question to be more specific on what I'm looking for. I am trying to find if the position of (1,2) is greater than (1,1). Using the following code I get a specific element, being what , and return the position. For example if we want to find 1 in this maze, than we do the following: findMaze([Row|_],What,(0,C)):- findRow(Row,What,C). findMaze([_|Rows],What,R):- findMaze(Rows,What,TempR), R is TempR + 1. findRow([What|_],What,0). findRow([_|Tail],What,Where):- findRow(Tail

Swapping a specific number in list 1 with a specific number in list 2

不想你离开。 提交于 2020-01-14 13:58:10
问题 I have been brushing up on some Prolog recently. I kind of enjoy just coming up with random problems to try and solve and then working them out. This one is quite tough though, and I'm not one to give up on a problem that I have set out to solve. The problem: I want to make a predicate that will have 2 predetermined lists, 2 numbers to swap, and then output the lists after the swapping is done. Further Explanation: I made it a little harder on myself by wanting to find a specific unique

Swapping a specific number in list 1 with a specific number in list 2

只谈情不闲聊 提交于 2020-01-14 13:58:08
问题 I have been brushing up on some Prolog recently. I kind of enjoy just coming up with random problems to try and solve and then working them out. This one is quite tough though, and I'm not one to give up on a problem that I have set out to solve. The problem: I want to make a predicate that will have 2 predetermined lists, 2 numbers to swap, and then output the lists after the swapping is done. Further Explanation: I made it a little harder on myself by wanting to find a specific unique