how to read numbers from file and sorting that in (prolog programming)
Asked
Active
Viewed 2,200 times
0
-
3What have you tried? If this is homework, show us an honest attempt and ask about specific problems you encounter. – Nifle Jan 07 '10 at 11:19
-
http://www.learnprolognow.org/ – Nifle Jan 07 '10 at 11:20
1 Answers
2
You can first try the following, reading multiple lines from the console:
?- repeat, read(X), (X==end_of_file, !, fail; true).
1.
X = 1 ;
2.
X = 2 ;
No
Explanation: The repeat/0 predicate repeatedly succeeds so that read/1 is called over and over. Calling read/1 only stops when end_of_file has been reached because of the cut that follows it.
Then you can wrap it into a findall/3 and call sort/2:
?- findall(X,(repeat, read(X), (X==end_of_file, !, fail; true)),L), sort(L,R).
2.
1.
L = [2, 1],
R = [1, 2]
If needed you can use your own sort and enhance the read by a stream argument.
Best Regards