问题
I have a Prolog sentence parser that returns a sentence (passed into it as a list) split into two parts - a Noun_Phrase and a Verb_Phrase. See example below:
sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))) :-
np(Sentence, Noun_Phrase, Remainder),
vp(Remainder, Verb_Phrase).
Now I want to take the Noun_Phrase and Verb_Phrase and pass them into another Prolog predicate, but first I want to extract the first term from the Verb_Phrase (which should always be a verb) into a variable and the rest of the Verb_Phrase into another one and pass them separately into the next predicate.
I thought about using unification for this and I have tried:
sentence(Sentence, sentence(np(Noun_Phrase), vp(Verb_Phrase))),
[Head|Tail] = Verb_Phrase,
next_predicate(_, Noun_Phrase, Head, Tail, _).
But I am getting ERROR: Out of local stack exception every time. I think this has something to do with the Verb_Phrase not really being a list. This is a possible isntance of Verb_Phrase:
VP = vp(vp(verb(making), adj(quick), np2(noun(improvements))))
How could I get the verb(X) as variable Verb and the rest of the term as varaible Rest out of such compound term in Prolog?
回答1:
You could use =../2 like:
Verb_Phrase=..[Verb|Rest_Term_list].
Example:
?- noun(improvements)=..[Verb|Rest_Term_list].
Verb = noun,
Rest_Term_list = [improvements].
来源:https://stackoverflow.com/questions/43836735/getting-hold-of-a-variable-in-complex-compound-term-in-prolog