prolog

Reversible tree length relation

点点圈 提交于 2020-01-11 05:25:14
问题 I'm trying to write reversible relations in "pure" Prolog (no is , cut, or similar stuff. Yes it's homework), and I must admit I don't have a clue how. I don't see any process to create such a thing. We are given "unpure" but reversible arithmetic relations (add,mult,equal,less,...) which we must use to create those relations. Right now I'm trying to understand how to create reversible functions by creating the relation tree(List,Tree) which is true if List is the list of the leaves of the

Breadth First Search in Prolog

佐手、 提交于 2020-01-11 03:32:28
问题 I'm new to Prolog and currently implementing DFS (depth-first search) and BFS (breadth-first search) algorithms. My DFS works fine as the code below, but the BFS is terminated and aborted when it reaches the leaf node (it doesn't backtrack and continue searching). I also read some sample code about this but there are some functions they don't define like s(Node, NewNode)... so it's hard to understand, or the version use Queue is too complicated. Here is my code: Some ground functions:

Example channelling constraints ECLiPSe

情到浓时终转凉″ 提交于 2020-01-11 01:41:17
问题 Can someone provide a simple example of channelling constraints? Channelling constraints are used to combine viewpoints of a constraint problem. Handbook of Constraint Programming gives a good explanation of how it works and why it can be useful: The search variables can be the variables of one of the viewpoints, say X1 (this is discussed further below). As search proceeds, propagating the constraints C1 removes values from the domains of the variables in X1. The channelling constraints may

Gathering all solutions without findall

∥☆過路亽.° 提交于 2020-01-10 05:55:07
问题 So as part of my work my code needs to print all the solutions to a query but without using the findall/3 predicate. I've done some reading around and there are ways involving adding the solutions to a list and so on. I tried doing this myself but to with no success; hence I was hoping someone may be able to show how I would print all the solutions without using findall. The program code is as follows: solutions(Q, 100):- Q = [X, Y, S], between(2,50,X), between(2,50,Y), S is X+Y, Y > X, S =<

Optimising a Prolog Program (remove duplicates, repeat recalculation)

我只是一个虾纸丫 提交于 2020-01-07 06:45:39
问题 I am very inexperienced with Prolog. I have a data set that contains elements and relations in graph that has circularity (quite a lot). There are rules to calculate the summary relation of a path. One of these is: one takes the path, then takes the weakest relation, and that is the one that holds between both ends. With Elements E1 , E2 , E3 and Relations R1/R1c , R2 , R3 (strength low to high) and structure E1-R3-E2 , E1-R1-E2 , E2-R2-E3 , E3-R1-E2 I can make the following minimal example:

right linear context free grammar

好久不见. 提交于 2020-01-07 06:38:28
问题 I've a problem. I have to write right linear context free grammar with alphapet={0,1} where the numbers of 0 will be even and the numbers od 1 will be odd. I tried write sth. but it's doesn't work. s --> [1],a. s --> [0],b. a --> []. a --> [1],c. a --> [0],b. c --> [1],k. c --> [0],b. b --> [0],k. b --> [1],d. d --> [1],b. d --> [0],c. k --> []. k --> s. Grammar should accept even amount of 0s and odd amount of 1s. Grammar context free is right linear when A->wB or A->w where w is any word

Where did my logic go wrong?

狂风中的少年 提交于 2020-01-07 05:08:07
问题 as a beginner in prolog with only previous knowledge in Java programming, I find it hard to relate a logic into a prolog rule... Below is what I have this far, and I would like anyone to point out where I went wrong, considering the results that I got. shapes(X):-triangle(X); circle(X); quadrilateral(X); withColour(X). square(sq). rectangle(rect). circle(cir). triangle(tri). quadrilateral(X). quadrilateral(X):-square(X). quadrilateral(X):-rectangle(X). red(X):-quadrilateral(X). yellow(X):

Prolog Type Error

霸气de小男生 提交于 2020-01-07 04:30:12
问题 I'm working on a prolog algorithm that will perform a "swap" on a list. Example: Input: [1,2,3,4] -> Output: [3,4,1,2] Input: [1,2,3,4,5] -> Output: [4,5,3,1,2] The first half and second half of the list swap places, if there is an odd number then the center element retains it's position. I have come up with an algorithm, but I am getting an error: ?- swap([1,2,3,4],L). ERROR: length/2: Type error: `integer' expected, found `round(4/2)' My code is as follows: swap(L, S) :- length(L, Length),

replacing elements between two lists using prolog

纵饮孤独 提交于 2020-01-07 03:46:12
问题 My task is to make myReplace(E1,L1,E2,L2) such that the very first occurrence of E1 in L1 gets replaced by E2 and is returned in L2 . I have written the below described code and it is working properly. myReplace(E1,[],E2,[]). myReplace(E1,[E1|Xs],E2,[E2|Ys]):- myReplace(E1,Xs,E1,Ys). myReplace(E1,[H|Hs],E2,[H|Ts]):- E1 \= H, myReplace(E1,Hs,E2,Ts). However, For example myReplace(2,[1,2,3,2,1],5,X) should give X = [1,5,3,2,1] and X = [1,2,3,5,1] . But my code is only giving one solution which

Returning results in recursion

醉酒当歌 提交于 2020-01-07 02:43:08
问题 I am trying to solve a problem where I need to evaluate the value of a binary expression tree. tree_calc(Tree, Eval) where Eval should hold the final result and Tree must be in the form: tree(LeftNode, Operator, RightNode) If I create the tree function as per the form above, how am I supposed to pass the results of the calculations back up the recursion if there isn't an empty variable to store the results? My understanding is that an extra variable is always needed to store the result in.