我正在寻找 swipl
与 perl -e
相似的功能
特别是,我想以这种方式运行 prolog 代码:
swipl --wanted-flag "fact(a). message:-writeln('hello')." -g "message" -t halt
这可能与
swipl -f file -g "message" -t halt
其中 prolog 子句写在
file
中我在服务器端运行 swipl,它将用户输入作为 prolog 子句,因此在服务器上编写文件不是一个好主意。
最佳答案
您可以做的一件事是使用 load_files/2
with the option stream
,并从标准输入加载,而不是从参数加载(我猜你仍然可以将入口点作为参数传递):
在文件中说 fromstdin.pl
你有:
main :-
load_files(stdin, [stream(user_input)]),
current_prolog_flag(argv, [Goal|_]),
call(Goal),
halt.
main :- halt(1).
有了这个,你可以做到:
$ echo 'message :- format("hello~n").' | swipl -q -t main fromstdin.pl -- message
|: hello
@false 对 this answer 的评论这个问题会告诉你这是什么
|:
是,如果您想知道,但如果它让您烦恼,请执行以下操作:$ echo 'message :- format("hello~n").' \
| swipl -q -t main fromstdin.pl -- message \
| cat
hello
反而。
这将让您从标准输入读取任何 Prolog 并从中调用任意谓词。这是否是一件聪明的事情,我不知道。如果有更简单的方法来实现同样的目标,我也不会感到惊讶。
关于command-line - 在命令行中使用 swipl 运行 prolog 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30769440/