Sort code in Prolog [closed] - sorting

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm writing a program in prolog and need some help. This could probably be considered a homework question.
I have two sort algorithms written and working. My goal is to pass in a list, and if it is greateror equal to than length 5, use sort method 1. if it's less than 5, use sort method 2. The sort methods both return a sorted list, and the main method that they are used in should return the sorted list.
here's the main method that I'm working with:
%Main method to run program
main(List, Result) :-
size(List, N),
( N => 5;
sort1(List, X),
sort2(List, X)).
Every time I try to run this, though, I get an error:
?- main([1,5,1,4,2,6,1]).
ERROR: Undefined procedure: main/1
ERROR: However, there are definitions for:
ERROR: main/2
false.
I'm not sure what this error means. Could anyone help me please?

You must correct the syntax. From your description:
main(List, Result) :-
length(List, N),
( N >= 5
-> sort1(List, Result)
; sort2(List, Result)
).

Related

How can I compare items in a Prolog database?

I have a Prolog database that is
dateopened(asda,date(1985,12,5)).
dateopened(tesco,date(1979,12,17)).
dateopened(morrisons,date(1999,12,25)).
dateopened(sainsburys,date(1979,12,17)).
dateopened(lidl,date(1987,8,27)).
I want to find out how to ask the following questions (Prolog queries) to answer the following:
Are there any two distinct supermarkets that opened on the same day and if there are, what are their names?
(I have no idea how to compare items in a database)
Give a year in the 1990s when no supermarkets were opened.
I have tried:
?- dateopened(Supermarket,date(Year,_,_)),Year>1989, Year<2000.
And the result I get is:
Supermarket = morrisons, Year = 1999.
Which sort-of answers the question because I can say that no supermarkets were opened in 1998 or 1997 etc but I don't think this is what is required.
There are a few clues, the questions can be answered using member/2, not/1 and \=.
It's a beginner querying exercise but I have no idea how to start, especially question 1.
As this is a learning exercise, my answer is a little more general, not with a concrete solution:
Question 1: The Prolog interpreter seeks to resolve a query by unifying it with the facts and rules in the fact basis, which form a closed world of known things. To check if something is true according to the facts you just state that in the query so that the solver can try to unify with the facts, using variables in places where you want to obtain a value to work with, and _ as a placeholder for variables whose actual values you don't need. The following query gives you any supermarket, with the full date in a variable.
?- dateopened(S1,Date1).
S1 = asda,
Date1 = date(1985, 12, 5) ;
...
If your query needs multiple conditions met you can combine them with ,, evaluated from left to right. To solve the initial question, you just pick any two supermarkets in the same way using different variables, and afterwards make sure their date is equal and their name is different \=, which reduces the number of possible solutions to what you need.
Question 2: I think your idea is almost of the opposite of what you want, but not really, as the opposite of 1999 would be all the other years. As a solution sketch:
Find a year in the nineties. The predicate between/3 will help, but if you are only allowed the above ones, a list L with the years and member(Year,L) will do.
Use the first part of your initial attempt to reduce the possible years to those when a super market was opened.
Invert that last part using \+ (not/1 is deprecated, assuming SWI-Prolog) so that you find the other years for which we previously made sure they were in range.

Prolog, New in Prolog and getting syntax error [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm really new in Prolog (maybe few hours) and I'm getting these errors and warnings, most importantly please tell how to fix that error and where it comes from.
My program
This language is so vague.
Thank you.
:- redefine_system_predicate(legalTime(T)).
legalTime(T,[T|_]).
legalTime(T,[_|S]) :- legalTime(T,S).
:- redefine_system_predicate(subset(_,_)).
subset([],_).
subset([T|S],K) :- legalTime(T,K), subset(S,K).
disjoint([],_).
disjoint([T|S],K) :- not(legalTime(T,K)), disjoint(S,K).
:- redefine_system_predicate(union(_,_,_)).
union(L,K,M) :- append(L,K,M).
:- redefine_system_predicate(intersection(_,_,_)).
intersection([],_,[]).
intersection([T|S],K,M) :-not(legalTime(X,K)), intersection(S,K,M).
intersection([T|S],K,[X|M]) :- legalTime(T,K), intersection(S,K,M).
difference([],_,[]).
difference([T|S],K,M) :- legalTime(X,K), difference(S,K,M).
difference([X|L],K,[X|M]) :- not(legalTime(X,K)), difference(L,K,M).
:- redefine_system_predicate(legal_Schedule(X)).
legal_Schedule(X,[X|_]).
legal_Schedule(X,[_|S]) :- legal_Schedule(X,S).
Schedule(C, R, between(S, E)) :- Schedule(C, R, between(S, E)), between(S, E).
Schedule(C, R, between(S, E)) :- Schedule(C, R, between(S, E)), C depends on R.
Schedule(C, R, between(S, E)) :- Schedule(C, R, between (S, E)), duration(C)=<difference(E,S).
This is the error I'm getting:
Warning: c:/users/amir-i7/desktop/hwk11.pl:24:
Singleton variables: [X]
ERROR: c:/users/amir-i7/desktop/hwk11.pl:28:8: Syntax error: Operator expected
ERROR: c:/users/amir-i7/desktop/hwk11.pl:29:8: Syntax error: Operator expected
ERROR: c:/users/amir-i7/desktop/hwk11.pl:30:8: Syntax error: Operator expected
% c:/Users/Amir-i7/Desktop/hwk11.pl compiled 0.02 sec, 16 clauses
You have a lot of problems here.
Predicates must begin with a lowercase letter.
I'm talking about Schedule on the last three lines, which are syntactically incorrect. You probably mean schedule here and this is probably the source of the syntax error that brought you here.
You don't need to warn about overwriting system predicates if your predicates are not system predicates.
Prolog doesn't ship with legalTime/1, legal_Schedule/1 so these directives don't do anything. Also, the predicates union/3, intersection/3 and subset/2 are not system predicates—if they are provided (as in library(lists) with SWI) they aren't part of the global scope. So you should probably forget you ever saw this directive—even if you were using it correctly, it's a code smell.
More to the point, these directives do not apply to the predicates that follow them. I think you've assumed that the "return value" or something doesn't "count" as part of the arity, but Prolog doesn't have "return values" in the sense that other languages do; the formal parameters encompass all of the inputs and outputs from the predicate. Your declarations for subset/2, union/3 and intersection/3 are correct, but you have the wrong arity for legalTime and legal_Schedule and because you used a non-anonymous variable in the latter you are getting a singleton variable warning.
Unbounded recursion in "Schedule/3*".
Your rules follow this general shape:
predicate(V1, V2... VN) :- predicate(V1, V2... VN), ...
This is a meaningless infinite loop. I think you need to delete the part after the :- that repeats the head of the clause. If there's a reason you need to recur, you should probably do it after the tests you do, and not on exactly the same input.
There are no "functions" in Prolog.
The statement duration(C) =< difference(E, S) isn't going to do what you want. Again, Prolog doesn't have return values, so this isn't going to magically send E and S through your difference/3 predicate and substitute in the third argument here as a value. More to the point, Prolog simply doesn't evaluate terms like this. I bet you have a duration/2 predicate somewhere else not included in your code sample and have the same expectation there. If this were to be rewritten as actual Prolog code, it would probably wind up looking something like this instead:
duration(C, Duration),
difference(E, S, Difference),
Duration =< Difference
I expect you'll be surprised by this. It's not Prolog but other languages that are vague. Prolog is actually simple and extremely well-defined when it comes to the shape of expressions and how they are evaluated. Other languages have a lot of special rules that you've internalized; Prolog is just going the other way, so it's confusing you. But it isn't vague. (And you may find you get better help if you avoid denigrating the thing you need help with—after all, the help is going to come from people who like it.)
Your largest problem here is that you expect Prolog to work like other programming languages. Probably you have a really dense class schedule and just haven't been able to give Prolog the attention it requires. There is no quick and easy remedy for this. You just need to crack open your copy of Programming in Prolog or Art of Prolog and reread those boring first chapters again. I promise, you will be surprised.
In the future, I would recommend that you include all of the code, not just the portion failing for you. I have lots more compile problems than the ones you asked about specifically because of this. When you don't really have a question other than "someone please make this work!" it is extremely hard to help without all of the code.

How to decrypt this transposition cipher? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My problem is that I am trying to decrypt following cipher text:
AWGNEYDTBSREAHGIDEIEEIHKIWABHPRCROIWSSBHTE
OCSNASLAIGTTOTYIOBNANNOOCHENEHSADUAIEOSNTH
TEOOAWUTOVTISNTVIFIHSNADVOGOREAPTBETSIT
DCSNASAAHOOULITUREICBNOERSNETTNOITTNTSDEOO
RERNINTOVTINTEFKGITPRCROEEIGREIGREHKFN
HOCWHPDTOSREGTOUREINDIUATHCHIEOSN
I know that it is English language. I know that it was encrypted only once by substitution, monoalphabetic or transposition.
I checked the Index of Coincidence and I received: 0.06833754056978002
I also checked frequency of letter and I received:
T 11.02, E 10.59, O 10.16, I 9.74, N 8.47, S 6.77, A 6.35, R 5.93, H 5.93, G 3.38, D 3.38, C 3.38, U 2.54, B 2.54, W 2.11, V 1.69, P 1.69, K 1.27, F 1.27, Y 0.84, L 0.84.
So I claim that it is transposition cipher. Am I right?
I checked all combinations of Rail Fence Transposition. I did not receive something sensible.
I checked all combinations of Columnar Transposition without changing order. I did not receive something sensible.
I checked a few combinations of Columnar Transposition with changing order. I did not receive something sensible.
Do you have idea how I can resolve this interesting task?

Password Algorithm [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Could anyone tell me what type of Algotithm/Hash Function the following passwords use?
password: scrambled text
hhhhhhhhhhhhhhh : .!_-*/P_.!_-*/P
iiiiiiiiiiiiiii : / ^,+.Q^/ ^,+.Q
jjjjjjjjjjjjjjj : ,#]/(-R],#]/(-R
kkkkkkkkkkkkkkk : -"\.),S\-"\.),S
lllllllllllllll : *%[).+T[*%[).+T
It just looks like
char[i] = someMap[i%6, char[i%6]]
and the someMap[i] submaps are sequentially taken from ascii.
You don't have to guess the exact formula to reproduce it : a char of ascii code c at index i%6 always give the same char, so, with 127 tests (or more if you want to handle more chars), you can build your own map of dimension 127 x 6.
Note that it's impossible with your small test data to be sure there is no cumulative function in which a permutation could be function of the precedent char.

What is the difference between loop and iteration? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
What kinds of data structure are used to implement loop and iteration?
Loop refers to the code...
iteration refers to the process in which the content of loop is executed once..
so 1 iteration refers to 1 time execution of loop..
so basically one loop can undergo many iterations..
loop is used for fixed no. of element or certain condition occurs while iterator is used for itearing element dynamically that have different no. of elements at runtime
Only such of kind 1.
Some have tried using data structures of kind 42 for this purpose, but failed miserably.
Iteration is simply the number of time/times a loop can be executed, while loop is the code which generate or causes expressions to be iterated iteration when the loop is executing.
An example of lines of code in c++:
for(i=1; i<=10; i++)
{
cout<<"this is printed 10 times";
}
The above code is a for_loop in which the execution of the statement "this is printed 10 times" will be iterated/repeated 10 times.

Resources