在序言中将事实断言到文件中 [英] assert fact into file in prolog
本文介绍了在序言中将事实断言到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不删除先前事实的情况下将事实声明到文件中?
How can I assert a fact into a file without deleting the previous fact?
在以下行中,当我执行两次时,第二个事实覆盖了第一个事实:
In the following line, when I execute it twice, the second fact overwrites the first fact:
tell('animal.txt'),write(Animal),nl,told.
但是当我使用 assert
或 assertz
时,它什么也不做.
But when I use assert
or assertz
it will do nothing.
请帮帮我.
谢谢:)
推荐答案
tell
截断您正在写入的文件.
tell
truncates the file you're writing to.
使用 append('animal.txt')
代替.这将写入文件的末尾.
Use append('animal.txt')
instead. That will write to the end of the file.
回复您的评论:
我可以把它放在哪里?
你是说 append/1
吗?
您问题中的代码不应该包含在您的 type/2
定义中吗(用追加替换告诉)?例如
Shouldn't the code in your question go in your type/2
definitions (with append replacing tell)? E.g.
type(1, Name) :-
append('animal.txt'),
write(mammal(Name)), nl,
told.
这篇关于在序言中将事实断言到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文