简体   繁体   English

如何将计算结果写入Prolog中的文件?

[英]How to write the result of a calculation to a file in Prolog?

I wish to export the result of a calculation to a file of text in Prolog, attempting the following: 我希望将计算结果导出到Prolog中的文本文件,尝试以下操作:

I compile the following program test.pl : 我编译以下程序test.pl

write_to_file(File, Text) :-
   open(File, write, Stream),
   write(Stream, Text), nl,
   close(Stream).

I do the following: 我执行以下操作:

Y is 2**10000.

And finally: 最后:

write_to_file('test.txt',Y).

But the contents of the file test.txt are as follows: 但是文件test.txt的内容如下:

_306

What am I doing wrong? 我究竟做错了什么?

If you want to reuse a variable in different calls to the Prolog top-level, you need to prefix that variable with the $ character. 如果要在对Prolog顶层的其他调用中重用变量,则需要在变量前加上$字符。 For your example: 例如:

?- Y is 2**10000.
?- write_to_file('text.txt', $Y).

This is not needed if you make the two calls at one, using Prolog conjunction ( , ): 如果使用Prolog连词( , )一次进行两个调用,则不需要此操作:

?- Y is 2**10000, write_to_file('text.txt', Y).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM