prolog

Prolog - Explain trace steps in English

岁酱吖の 提交于 2019-12-24 11:48:09
问题 plays(alice, leadguitar). plays(noah, drums). plays(mike, leadguitar). plays(mike, drums). plays(katie, baseguitar). plays(drew, leadguitar). plays(drew, baseguitar). duetwith(Person1,Person2):- plays(Person1,L), plays(Person2,L), Person1 \= Person2. I write a new rule called combo that determines whether or not three people can make a combo with a drummer, a base guitar, and a lead guitar. combo(Person1,Person2,Person3):- plays(Person1,X), plays(Person2,Y), plays(Person3,Z), X \= Y, Y \= Z,

Another rule as argument to a rule in prolog

≯℡__Kan透↙ 提交于 2019-12-24 11:35:39
问题 This is my prolog file. male(bob). male(john). female(betty). female(dana). father(bob, john). father(bob, dana). mother(betty, john). mother(betty, dana). daughter(X, Y) :- female(X), mother(Y, X). I want to query something like this daughter(X, mother(Y, john)). Is it possible? I'm trying to get daughter of john's mother. I got this idea from here under 'Asking Questions with Structures' 回答1: Something like that ? daughter(X, Y), mother(Y, john). This will match Y as the mother of john and

Prolog - Substitute atom with value and evaluate

橙三吉。 提交于 2019-12-24 11:35:07
问题 Given Result = 4* (3*x)^3*3, and VarValue = x:2, How can I get the following output Value = 2592 ; if I have to defined the following predicate: evaluate(Result,Value,VarValue) I tried doing the following: evaluate(Result, Value, VarValue) :- member(VarValue, [x:X]). and trying to substitute X into the equation.. I could not think of a nice way to go from there. EDIT: Is there a way to only use the following built-predicates : //, /, +, -, ^, *,=..,>, <, atom, is_list, functor, arg, integer,

How to count the number of children of parents in prolog (without using lists) in prolog?

纵然是瞬间 提交于 2019-12-24 11:09:52
问题 I have the following problem. I have a certain number of facts such as: parent(jane,dick). parent(michael,dick). And I want to have a predicate such as: numberofchildren(michael,X) so that if I call it like that it shows X=1. I've searched the web and everyone puts the children into lists, is there a way not to use lists? 回答1: Counting number of solutions requires some extra logical tool (it's inherently non monotonic). Here a possible solution: :- dynamic count_solutions_store/1. count

Return vowels from word

旧巷老猫 提交于 2019-12-24 10:41:33
问题 I've got this work to do and my teacher gave me a prolog file with the following facts: vowel(a). vowel(e). vowel(i). vowel(o). vowel(u). consonant(b). consonant(c). consonant(d). consonant(f). consonant(g). consonant(h). consonant(j). consonant(k). consonant(l). consonant(m). consonant(n). consonant(p). consonant(q). consonant(r). consonant(s). consonant(t). consonant(v). consonant(w). consonant(x). consonant(y). consonant(z). And I need to create a rule that is able to return the vowels.

How do I read in a text file and print them out to a file in Prolog?

拜拜、爱过 提交于 2019-12-24 10:18:08
问题 I have a text file, and I want to read it in and print them out in screen and write them into a new output file. So what I have done so far is main :- open('text.txt', read, ID), % open a stream repeat, % try again forever read(ID, X), % read from the stream write(X), nl, % write to current output stream X == end_of_file, % fail (backtrack) if not end of !, close(ID). But I only received an error message like, ERROR: text.txt:1:0: Syntax error: Operator expected What should I do? 回答1: read/2

Going through lists Prolog

北城以北 提交于 2019-12-24 10:15:07
问题 I'm trying to work in Prolog and I'm having trouble understanding how to solve my problem. What I am trying to do is create tuples of 3 elements each one from a different list. What I need to do is make a tuple of three for every possible combination out of three lists. My plan is to take the first element in two of the lists and then go thru every element in the third list creating a tuple for each. Then take the first element in the first list and the second element in the second list and

How to write kind of Conditional Planning in Prolog?

☆樱花仙子☆ 提交于 2019-12-24 10:02:16
问题 I tried to write a prolog code that can understand student program written in C#. Now I'm stuck in the process of recognizing the 'if' statement in student's program. For example: The following is the code that I expect from the student. int d = int.Parse(Console.ReadLine()); // value d is inputted by user int s = 0; if (d>0) s = 2; else if (d==0) s = 1; else s = 0; I defined the goal of this expected code as: goal:- hasVarName(Vid_s, s), hasVarName(Vid_d, d), hasVarValue(Vid_d, Vd), ((not(gt

Foreach not working in Prolog

寵の児 提交于 2019-12-24 09:49:11
问题 I am trying following code, where foreach and string_codes are working separately: 7 ?- string_codes("acid", D). D = [97, 99, 105, 100]. 8 ?- string_codes(S, [116, 101, 115, 116]). S = "test". 15 ?- foreach(member(S, ["test", "acid"]), writeln(S) ). test acid true. But not together: 14 ?- foreach(member(S, ["test", "acid"]), string_codes(S, X) ). false. 17 ?- foreach(member(X,[[116, 101, 115, 116], [97, 99, 105, 100]]), string_codes(S, X)). false. Only first letter is printed with this code:

prolog, user choice coffee menu

纵饮孤独 提交于 2019-12-24 08:38:33
问题 I'm a beginner in Prolog course, I am trying to write code to allow the user to choose the coffee then ask hot or cold then the size of the coffee to calculate price. I was looking on the web explain in how to develop the program but I feel it's different from what I need in the example: [animal identification][1]. Can you please help me to write the coffee menu. Here is what I have tried. go :- hypothesize(Coffee), write('Your order is : '), write(Coffee), write('and the price for your order