Translation to DCG Semicontext not working

后端 未结 1 482
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 09:26

Since this question uses list, I wanted to solve it using DCG. In the process I realized that semicontext could be used. (DCG Primer)

The original problem is to return c

相关标签:
1条回答
  • 2021-01-25 10:07

    An alternative solution that doesn't require push-back lists or lookahead:

    count_dcg(N0,N) -->
        [C], {N1 is N0 + 1}, count_dcg(N1,N,C).
    count_dcg(N,N) -->
        [].
    
    count_dcg(N0,N,C) -->
        [C],
        count_dcg(N0,N,C).
    count_dcg(N0,N,C) -->
        [C1],
        {C \== C1, N1 is N0 + 1},
        count_dcg(N1,N,C1).
    count_dcg(N,N,_) -->
        [].
    
    count(L,N) :-
        phrase(count_dcg(0,N),L).
    
    0 讨论(0)
提交回复
热议问题