prolog

Retrieve Items in Prolog

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-13 07:46:29
问题 Say I have this knowledge base: step('pancakes', 1, 'mix butter and sugar in a bowl', [butter, sugar], [bowl]). step('pancakes', 2, 'add eggs', [eggs], []). step('pancakes', 3, 'mix flour and baking powder', [flour, baking powder], []). How do I make a predicate that retrieves all the ingredients for all the steps of the recipe? So if I would make a rule retrieveIngredients(X,Y). and ask retrieveIngredients('pancakes',Y). , how would I be able to make it retrieve Y = ['butter','eggs','flour',

prolog graph depth first search

梦想的初衷 提交于 2020-12-13 07:18:15
问题 I've seen other questions about depth first search but my problem is slightly different and I really don't get it. In prolog, I represent my undirected graph like this: [0-[1,5], 1-[0,2], 2-[1,3], 3-[2,0], 5-[0]] Which is a set of key-value, where the key represents the node and the list: -[] represents its neighbors. I don't know how to do a depth first search with this model. I've tried many solution. I want a really basic recursive algorithm like this one: dfs(v, visited): if visited[v]

prolog graph depth first search

为君一笑 提交于 2020-12-13 07:17:05
问题 I've seen other questions about depth first search but my problem is slightly different and I really don't get it. In prolog, I represent my undirected graph like this: [0-[1,5], 1-[0,2], 2-[1,3], 3-[2,0], 5-[0]] Which is a set of key-value, where the key represents the node and the list: -[] represents its neighbors. I don't know how to do a depth first search with this model. I've tried many solution. I want a really basic recursive algorithm like this one: dfs(v, visited): if visited[v]

prolog graph depth first search

旧街凉风 提交于 2020-12-13 07:16:54
问题 I've seen other questions about depth first search but my problem is slightly different and I really don't get it. In prolog, I represent my undirected graph like this: [0-[1,5], 1-[0,2], 2-[1,3], 3-[2,0], 5-[0]] Which is a set of key-value, where the key represents the node and the list: -[] represents its neighbors. I don't know how to do a depth first search with this model. I've tried many solution. I want a really basic recursive algorithm like this one: dfs(v, visited): if visited[v]

Retrieve constrained list from knowledge base

笑着哭i 提交于 2020-12-13 05:46:10
问题 step('pancakes', 1, 'mix butter and sugar in a bowl', [butter, sugar], ['bowl']). step('pancakes', 2, 'add eggs', [eggs], []). step('pancakes', 3, 'mix flour and bakingpowder', [flour, baking_powder], []). steps(R,Y,P):- findall(A,step(R,_,A,_,P),Z), distinctSteps(Z,Y). distinctSteps(Z,Y) :- list_to_set(Z,Y). So I have this knowledge base and a rule that should retrieve the step with the certain utensil I give as a parameter. So for example steps('pancakes', bowl, I). should return I = ['mix

Flattened form in WAM

微笑、不失礼 提交于 2020-12-12 10:45:10
问题 The WAM: A Tutorial Reconstruction states that a query, p(Z, h(Z,W), f(W)), needs to be flattened using the following principles: That being said, the query flattened form is: X3=h(X2, X5), X4=f(X5), X1=p(X2, X3, X4); I am lost with the definition of external variable, consider the following: p(Z, h(Y, a(K, C), b(W)), f(W)). Is Y an external variable? How should be the flattened form for this? From my understanding this would be the construction: X1 = p(X2, X3, X4) X2 = Z X3 = h(X5, X6, X7)

ListsfromList function in Prolog

ぃ、小莉子 提交于 2020-12-12 05:39:42
问题 I writing a predicate in prolog in which I give two parameters listsFromL(X,B) . X would be a list with lists within that list and B would be a new list with the lists from X . So, for example, if I would call listsFromL([1,2,[d,a]],X). the return would be B = [[d,a]]. and if I would add more lists to X I would get a longer list with lists as X . How do I go about this? 回答1: listsFromList([],[]) . listsFromList([HEAD|SOURCEs],[HEAD|TARGETs]) :- is_list(HEAD) , listsFromList(SOURCEs,TARGETs) .

ListsfromList function in Prolog

久未见 提交于 2020-12-12 05:38:03
问题 I writing a predicate in prolog in which I give two parameters listsFromL(X,B) . X would be a list with lists within that list and B would be a new list with the lists from X . So, for example, if I would call listsFromL([1,2,[d,a]],X). the return would be B = [[d,a]]. and if I would add more lists to X I would get a longer list with lists as X . How do I go about this? 回答1: listsFromList([],[]) . listsFromList([HEAD|SOURCEs],[HEAD|TARGETs]) :- is_list(HEAD) , listsFromList(SOURCEs,TARGETs) .

Prolog. Construct a transitive closure of a graph

喜夏-厌秋 提交于 2020-12-12 05:09:32
问题 I'm very new to Prolog. I have such a graph: edge(a,e). edge(e,f). edge(f,d). edge(d,a). I define a transitive closure as: p(X,Y) :- edge(X,Y). tran(X,Z) :- p(X,Y), p(Y,Z). I need to construct a transitive closure of a graph. Please let me know how to proceed with it. 回答1: The problem with trans/2 is that it will only walk two edges, not an arbitrary number. We can define a predicate tran(X, Z) that holds if edge(X, Z) holds, or edge(X, Y) and then tran(Y, Z) . We thus in the latter follow

Prolog - Recursively append numbers to a list

谁都会走 提交于 2020-12-11 08:59:56
问题 I am just starting to learn Prolog, and I am having troubles wrapping my head around recursive concepts. Right now, solely for the purpose of practice, I am trying to write a program that appends 10 numbers to a list and then prints out that list. The self-imposed rule for this program is that the list has to be 'declared' (I am not sure if that is the correct word for Prolog) in a main predicate, which calls another predicate to append numbers to the list. This is what I have so far, and I