-1
pc --> [ta] -> [tb] -> [tc] .

If I have tc , how do I get [ta,tb] put into a variable?

If I have the tail end of a list defined in a definite clause, how do I get the preceeding elements as a list of unknown length?

I am hoping that approach can be extended to a case where the prefix of a string known at the time of a query be extracted with the suffix removed.

For example, given "sweetness", use a DCG describing the suffix "ness" to bind "sweet" to a variable.

Do not only use 'append'.

The following examples are not part of the question, they are information for other people learning definite clauses up to this stage of complexity.

pa --> [ta] .

Can test with

phrase(pa,[ta]) .

To fill variable with match, do

phrase(ptrn,Variable) .

pb --> [ta] -> [tb] .

The head element can be gotten with

phrase(pb,[Head|[tb]]) .
lurker
  • 56,987
  • 9
  • 69
  • 103

1 Answers1

0

Assuming SWI-Prolog, from library(dcg_basics), there is string//1 non-terminal that should work the way you want:

?- use_module(library(dcg/basics)).
...

?- phrase((string(L), [pc]), [pa,pb,pc]).
L = [pa, pb] ;
false.

the implementation is quite simple:

string([]) -->
    [].
string([H|T]) -->
    [H],
    string(T).

OT: you're using ->//2 as a sequence, and albeit it should work for your simple case, be aware it really introduces the if/then/else construct.

false
  • 10,264
  • 13
  • 101
  • 209
CapelliC
  • 59,646
  • 5
  • 47
  • 90