how to read numbers from file and sorting that in (prolog programming)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
(c) 2010, Jan Burse, 8004 Zürich