Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
We have facts
studies(cse, plc).
studies(cse, da).
studies(it, se).
studies(it, plc).
where studies(x,y) means that branch x studies module y .
Now I Want to define Rule To count number of modules in all. like here it will be 3.that are (plc,da,se).PLZ HELP.
What will be the query to find how many subjects studies under CSE.
having tagged SWI-Prolog your question, take a look at library(aggregate):
?- aggregate(count, Module, Branch^studies(Branch, Module), N).
N = 3.
library(aggregate) is powerful, learning about it can be really rewarding...
I will not tell you the solution but this can help you to find it out by yourself:
If you want to count the modules then you need a list of modules and take its length.
Always remember this sentence:
A list is either an empty list or an element and a list.
Using this you can construct lists of your modules recursively.
Make sure, no element is in the list twice.
number_of_modules(N) :-
findall(M, studies(_,M), Ms),
sort(Ms, SortedMs),
length(SortedMs, N).
?- number_of_modules(N).
N = 3.
sort/2 removes duplicate elements.
The arguments of findall/3 are, from left to right, (1) a template for the answer which is to be collected, (2) the goal from which the answer is drawn, (3) the a list of all answers. So you could, for instance, label each module as such by using a different template in (1):
number_of_modules(N, SortedMs) :-
findall(module(M), studies(_,M), Ms),
sort(Ms, SortedMs),
length(SortedMs, N).
?- number_of_modules(N, Ms).
N = 3,
Ms = [module(da), module(plc), module(se)].
Documentation on this and related predicates can be found in section 4.3 of the manual.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
What is the meaning of this command in Prolog's commanf line? What does time and what does time(ids)?
?- time(ids)
Prolog lists are singly linked lists and it's more convenient and more performant to prepend things on the front instead of append them on the end. A common technique is to build a list backwards, then reverse it. Slago is doing that.
The search starts with the Start state in the list, prepends intermediate states, and finishes when goal(State) holds and the state at the front of the list is the solved puzzle.
Context: https://stackoverflow.com/a/67645940/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I currently see two ways to code the next step of my program and there are probably more, but the two routes I have are as follows.
I take the factors of the lowest number and loop through the other numbers two see if they share those common factors.
I find the factors of the lowest number and add it to a list. I then find the factors of the other numbers that do not exceed the lowest and add them to the same list. I then run through the list to check which is the highest number that appears x times.
I am leaning towards 1, but I'm not sure.
Sorry if this is too ambiguous, thanks.
Well, given the ambiguity, as stated: the 1st requires less steps and avoids the allocation of a data structure.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I sort facts on its variable Like
Student(5).
Student(3).
Student(2).
Student(3).
Student(6).
I want to make function to make them appear
Student(2).
Student(3).
Student(3).
Student(5).
Student(6).
I would first collect all these facts to a list using findall (example: How to create a list from facts in Prolog?) , and then sort this list (example: Sorting a list in Prolog , or just use the built-in sort/2 predicate ).
(Sent from my phone)
At the moment, they are not facts in proper Prolog, you need to write them with small letters:
student(5).
student(3).
% etc
Then, several things you can do:
?- findall(S, student(S), Students), msort(Students, Sorted).
(as suggested in the other answer)
If you want to have them actually sorted in the database, and are not afraid to change the database at run-time, you can then remove all student/1 from the database with abolish/1 and re-insert the sorted facts:
reorder_students :-
findall(S, student(S), Students),
msort(Students, Sorted), % do not remove duplicates
abolish(student/1),
forall(
member(M, Sorted),
assertz(student(M))
).
It is not a very good idea to do this repeatedly! If you have a regularly changing database of students you might consider not putting them in the database, but instead using for example an association list as in library(assoc)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a piece of code that says:
for i = 4,16, . . . , n
I am trying to find an upper bound in terms of big oh notation for the number of times the statement gets executed. I believe here it goes like 4,42,43 ... and so on. Since it grows exponentially, it looks like to me that that code is executed about O(logn) times. Am i right? Thanks in advance.
You can confirm your result by thinking in terms of a loop whose index variable is used as the exponent, taking the values 1, 2, 3, ... , floor(log_4(n))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Isaac and Albert were excitedly describing the result of the Third Annual International Science
Fair Extravaganza in Sweden. There were three contestants, Louis, Rene, and Johannes. Isaac reported
that Louis won the fair, while Rene came in second. Albert, on the other hand, reported that Johannes
won the fair, while Louis came in second.
In fact, neither Isaac nor Albert had given a correct report of the results of the science fair. Each of them had given one correct statement and one false statement. What was the actual placing of the three contestants? Please base your solution to a Prolog program.
Well, i am beginner at prolog and i want to interpret such paragraphs into prolog code but i am not sure how to approach to this. Can you lead me about that?
We start by recording the statements of Isaac and Albert. 1 and 2 are used to identify the statements ("the first statement of Isaac..."), each list represents the participants in their order.
isaac(1,[louis,_,_]).
isaac(2,[_,rene,_]).
albert(1,[johannes,_,_]).
albert(2,[_,louis,_]).
Next we say who participated in the fair and that any answer should be a permutation of the three names. I'm working with SWI prolog, so permutation is a built-in predicate:
domain([louis,johannes,rene]).
valid(X):- domain(D), permutation(D,X).
Finally, we put everything together:
go(X) :- isaac(I,X),
albert(J,X),
valid(X),
\+ (isaac(K,X), dif(I,K)),
\+ (albert(L,X), dif(J,L)).
Two last lines ensure that only one claim of Isaac (Albert) is true.