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)
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 to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
List like this:[1,[2,3,4]]
Remove the first element of sub-list which is 2,
Result like this:[1,[3,4]]
This should get you an idea of how to do it:
rem_sublist_first([], []).
rem_sublist_first([X|Y], [X|Y2]):-
\+ X = [_|_],
rem_sublist_first(Y, Y2).
rem_sublist_first([[_|Y1]|Y2], [Y1|Y3]):-
rem_sublist_first(Y2, Y3).
test:-
rem_sublist_first([1,[2,3,4]], [1,[3,4]]),
rem_sublist_first([1,2,3,[4,5,6]], [1,2,3,[5,6]]).
I advise you to study prolog lists, they are essential for any type of prolog program so it is a worth investment if you are goind to write prolog code.
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.
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
A hash table with 10 buckets with one slot per bucket is depicted .The Symbol S1 to S7 are initially entered using a hashing function with linear probing . The maximum no. of comparisons needed in searching an item that is not present??
I am unable to solve this question. Please explain me how it can be computed in simple language for a learner
Consider what happens when all symbols hash to the same number (say zero for simplicity). How many comparisons are required to insert S1, then S2, etc?
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 8 years ago.
Improve this question
I would like to write a predicate for tic-tac-toe game, that generate possible successor board configuration for given player and board. After typing this input:
next_board([[x,o,x],[x,x,o],[o,e,e]],x,N).
the output should be displayed as following:
N=[[x,o,x],[x,x,o],[o,e,e]];
N=[[x,o,x],[x,e,o],[o,x,e]];
N=[[x,o,x],[x,e,o],[o,e,x]];
To generate possible successor boards you basically have to "replace" single empty cells (marked with e) with your player chip.
At first sight you might be tempted to use select/4 but it would replace every empty cell in a row with your player chip. So, you might be better off using append/3 predicate with something like this:
next_board(Board, Player, NextBoard):-
append(Head, [Row|Tail], Board), % Take a single row from the board
append(RowHead, [e|RowTail], Row), % Take a single empty cell
append(RowHead, [Player|RowTail], NRow), % and replace it with your player chip
append(Head, [NRow|Tail], NextBoard). % Now build a next possible board
You might as well make recursive procedures to achieve the same logic without using append/3.