序言列表高原 [英] Prolog List Plateau

查看:13
本文介绍了序言列表高原的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚被介绍给prolog,试图完成一些简单的练习,但我一直在这个问题上卡住了.我正在尝试编写一个输出输入列表的所有子列表的程序,其中每个子列表的长度> 1,并且不能扩展到更大的子列表.它还将输出子列表列表中的起始位置.所以一个示例输出将是

Just got introduced to prolog, trying to get through some simple exercises, but I've been getting kind of stuck on this one. I'm trying to write a program that outputs all the sublists of the input list, where each sublist has length > 1 and it cannot be extended to a larger sublist. It will also output the starting position in the list of the sublist. So a sample output would be

| ?- plateau([a,a,b,2,2,2,a+1,a+1,s(1,2)], I, Len).
    I = 1,
    Len = 2 ? ;
    I = 4,
    Len = 3 ? ;
    I = 7,
    Len = 2 ? ;
    no

我仍然对整个声明式的事情感到很困惑,并且在切换出命令式模式时遇到了很多麻烦.我在想我想让我的程序做类似的事情

I'm still pretty confused by the whole declarative thing, and having a lot of trouble switching out of imperative mode. I'm thinking I want my program to do something like

program([H|T],I,L):-
    T = [H1|T1] %split the tail
    ([H] = [H1] -> Count is Count+1, program(T,I,Count) 
     %if First element = second element, recurse with new values
    ; length(T,Spot), 
      %get the spot where you are in the list, so we know where sublist starts
      program(T,Spot,L) %run again, from tail, since sublist didn't have another  element?
program([],I,L). %terminate when entire list has been run through?

所以这行不通,据我所知有几个原因.我没有重置计数",所以它可能会将所有子列表的值加起来?有没有办法解决这个问题?我的基本情况也可能不是我想要的——我只是不确定它应该是什么?我可能也错过了其他东西......非常感谢任何帮助!:)谢谢!

So this isn't working, from what I can tell for a couple reasons. I don't reset 'count', so its totaling up the values of all the sublists together maybe? Is there some way to work around for this? My base case might also not be what I want - I'm just not sure what it should be really? I'm probably missing other things too...any help is greatly appreciated! :) Thanks!

推荐答案

这里有很多复杂的答案.考虑一下这个不使用 DCG 或许多内置插件(对于初学者来说可能更简单):

There are quite a lot of complicated answers here. Consider this one which doesn't use DCGs or many built-ins (perhaps simpler for a beginner):

plateau([X|Xs], I, L) :-
    plateau(Xs, 1-1-X, I, L).

plateau([X1|Xs], I0-L0-X0, I, L) :-
    X0 == X1, !,
    NL0 is L0 + 1,
    plateau(Xs, I0-NL0-X0, I, L).

plateau(_, I-L-_, I, L) :-
    L > 1.

plateau([X|Xs], I0-L0-_, I, L) :-
    NI is I0 + L0,
    plateau(Xs, NI-1-X, I, L).

第一个子句设置累积 (index)-(length)-(sublist element) 元组的调用,作为学期.

The first clause sets up the call which accumulates the (index)-(length)-(sublist element) tuple, as a term.

如果下一个列表element 相同,则next 子句增加length(注意索引没有改变).

The next clause increments the length if the next list element is the same (note the index isn't altered).

只有在测试子列表元素运行是否被破坏时第二个子句失败时才调用第三个子句(因为剪切 !),并返回 length>1.

The third clause is called only if the second clause failed when testing if the sublist element run was broken (because of the cut !), and returns a solution iff the length of the run was > 1.

最后一个子句使 Prolog 能够回溯并从上次运行重新开始搜索.

The last clause enables Prolog to backtrack and re-start the search from the last run.

gusbro 的解决方案实际上非常接近这个... +1.

gusbro's solution is actually very close to this one... +1.

这篇关于序言列表高原的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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