title: extracting words from a prolog list
body: <p>I have been set a task of natural language parsing in Prolog. So far I have the program working to an extent. It will print a sentence, for example if I input a list of <code>[the, cat, sat, on, the, mat]</code> it will correctly output:</p>
  
  <pre><code>(noun_phrase(det(the), np2(noun(cat))), verb_phrase(verb(sat), pp(prep(on), noun_phrase(det(the), np2(noun(mat)))))) 
  </code></pre>
  
  <p>The next task I have to do is to extract the keywords from the sentence, ie extract the noun in the noun phrase, the verb in the verb phrase and the noun in the verb phrase, so I could return a list: <code>[cat, sat, mat]</code>. Could anybody give me a hand getting started because I'm very stuck with this. Thanks!</p>
  
  <p>My current code is:</p>
  
  <pre><code>sentence(S,sentence((NP), (VP))):-
     nl,
  np(S, NP, R),
  vp(R, VP, []),
  write('sentence('), nl, write('   '), write((NP))
        ,nl,write('    '), write((VP)),nl,write('  ').
  
  np([X | S], noun_phrase(det(X), NP2), R) :- det(X), np2(S, NP2, R). np(S, NP, R) :- np2(S, NP, R). np(S, np(NP, PP), R) :- append(X, Y, S), /* Changed here - otherwise possible endless recursion */ pp(Y, PP, R), np(X, NP, []).
  
  np2([X | R], np2(noun(X)), R) :- noun(X). np2([X | S], np2(adj(X), NP), R) :- adj(X), np2(S, NP, R).
  
  pp([X | S], pp(prep(X), NP), R):- prep(X), np(S, NP, R).
  
  vp([X | R], verb_phrase(verb(X)), R) :- /* Changed here - added the third argument */ verb(X). vp([X | S], verb_phrase(verb(X), PP), R) :- verb(X), pp(S, PP, R). vp([X | S], verb_phrase(verb(X), NP), R) :- verb(X), np(S, NP, R).
  
  det(the). det(with). noun(cat). noun(mat). verb(sat). prep(on). adj(big).
  </code></pre>