print_term 在 PROLOG 中写入文件

print_term writing to a file in PROLOG

在这个mcve]

test :-                         
  tell('junk.txt'),
  print_term(
       examples([this,is,a,really,long,example,list],
             [made,of,lists,atoms,and,including,terms(involving,parentheses,like,this)]),
       []),
  told.

SWI-PROLOG 确实以 print_term 的方式将术语整齐地打印到屏幕上,但不会将其打印到文件中。如何重定向输出?

您可以在其 Options 参数中使用 current_output/1 to get the current Stream and then pass it to print_term/2

test :-                         
  tell('junk.txt'),
  current_output(Stream),
  print_term(
       examples([this,is,a,really,long,example,list],
             [made,of,lists,atoms,and,including,terms(involving,parentheses,like,this)]),
       [output(Stream)]),
  told.

我相信所有那些处理文件的爱丁堡风格谓词都处于维护模式,您现在可能更喜欢使用 ISO stream predicates