【发布时间】:2014-07-02 20:49:03
【问题描述】:
我希望在子列表中搜索特定字符串。有一个字符串列表(可能重复);我有一些物品;我有一个指向当前所需项目的索引;我想从索引搜索到列表的末尾,并知道 Item 是否还在。
我在这里找到的最相似的东西是Prolog: Create sublist, given two indices,但我不需要 2 个索引,我觉得它不应该那么复杂;这看起来也很接近,但我并没有真正理解它Create a sublist from a list given an index and a number of elements. Prolog。我也不认为 sublist/3 会这样做(而且它看起来已经被弃用了 - http://www.swi-prolog.org/pldoc/man?predicate=sublist/3)
例如,一个列表 [Blue, Orange, Red, Yellow, Orange],一个 sequenceIndex(2),一个 'Orange' 项,我想知道 Orange 在子列表 [Red, Yellow, Orange] 中。
所以,像这样,我猜这不是如何使用 nth:
sequenceIndex(0).
sequence([blue, orange, red, yellow, orange]).
stillThere(Color) :-
sequenceIndex(Index),
sequence(Listcolors),
nth(Index, Listcolors, Color).
stillThere(Color) :-
sequenceIndex(Index),
N is Index+1,
sequence(Listcolors),
nth(N, Listcolors, Color).
我会用have(X), stillThere(X) 调用它,其中X 是blue(等等。它会在整个程序中发生变化)。
非常感谢!
【问题讨论】:
标签: prolog