【问题标题】:How do I store and access a list within a variable in Prolog?如何在 Prolog 的变量中存储和访问列表?
【发布时间】:2010-10-05 19:14:56
【问题描述】:

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

% 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 中调用以下内容:

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

目标是避免在 Prolog 解释器中不断输入长列表,而是将它们存储在 .pl 文件中。但是,输入上面的命令会导致列表没有根据给定的子句正确检查。这如何实现,最好使用上面的结构来存储列表 {listContents([a,b,c,d]).}?

【问题讨论】:

    标签: list variables prolog


    【解决方案1】:

    我认为您需要将 Prolog 中的调用修改为

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多