문제

With a list containing only 0 and 1, it is required to find out whether each occurance of [0, 1] is followed (not necessarily immediately) by an occurance of [1, 0, 0]. How to do that?

도움이 되었습니까?

해결책

Instead of defining an automa, you could use a builtin, and the search capability of Prolog.

append/3 establish a relation among 3 lists. Namely, the third list is the concatenation of the first two. This can be used to answer easily your assignment:

satisfy(L) :- append(_, [0,1|R], L), append(_, [1,0,0|_], R).

as you can see, append(_, PatternAndRest, L) search the pattern in L and yields the Rest.

But you are probably required to write a recursive predicate that scan the list looking for pattern. I hope the syntax you can see in satisfy/1 will help you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top