在序言和排序中从文件中读取数字 [英] read numbers from file in prolog and sorting

查看:46
本文介绍了在序言和排序中从文件中读取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从文件中读取数字并在(序言编程)中对其进行排序

how to read numbers from file and sorting that in (prolog programming)

推荐答案

您可以先尝试以下操作,从控制台读取多行:

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

说明:repeat/0 谓词重复成功,使得read/1 被一遍又一遍地调用.调用 read/1 仅在 end_of_file 时停止已经到达,因为它后面的切割.

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.

然后你可以把它包装成一个 findall/3 并调用 sort/2:

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.

最好的问候

这篇关于在序言和排序中从文件中读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆