如何在Prolog中存储和访问变量中的列表? [英] How do I store and access a list within a variable in Prolog?

查看:75
本文介绍了如何在Prolog中存储和访问变量中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习Prolog的基础知识.我遇到了一个看似简单的问题,即无法在规则中存储列表并无法将其检索以供其他子句使用.例如:

I am attempting to learn the basics of Prolog for a class. I'm running into the seemingly simple problem of not being able to store a list within a rule and retrieve it for usage in other clauses. For example:

% These are the contents of the pl file I want to consult

% Numbers I want to process
inputList([3,2,1,0]).

% Prints out the contents of a list
printList([First | Tail]) :- 
    write(First),nl,
    printList(Tail).

我想做的是在Prolog中调用以下内容:

What I want to do is call the following within Prolog:

?- inputList(X).
?- printList(X).

目标是避免在Prolog解释器中不断输入长列表,而将它们存储在.pl文件中.但是,在上面输入命令会导致未根据给定的子句正确检查列表.

The goal is to avoid constantly entering long lists into the Prolog interpreter and instead store them in the .pl file. However, entering the commands above results in the list not being properly checked against the given clause. How can this be accomplished, preferably using the structure above to store a list {listContents([a,b,c,d]).}?

推荐答案

我认为您需要将Prolog中的通话修改为

I think you need to modify your call in Prolog to

?- inputList(X), printList(X).

这篇关于如何在Prolog中存储和访问变量中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆