简体   繁体   中英

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:

I compile the following program 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:

_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. 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 ( , ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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