Search code examples
syntaxprologdcg

prolog,very simple dcg syntax


I am trying to understand the basic syntax of prolog and dcg but it's really hard to get ahold of proper information on the really basic stuff. Take a look at the code below, I basically just want to achieve something like this:

Output = te(a, st).

Code: 
    test(te(X,Y)) --> [X], test2(Y).
    test2(st(_X)) --> [bonk]. 

    ?- test(Output, [a, bonk],[]).
    Output = te(a, st(_G6369)). 

Simply what I want to do is to add the the word 'st' at the end, and the closest way I've managed is by doing this but unfortunately st is followed a bunch of nonsense, most likely because of the singleton _X. I simply want my Output to contain like: te(a, st).


Solution

  • If you want to accept input of the form [Term, bonk] and obtain te(Term,st) you should change test/2 to accept bonk a return st:

    test(te(X,Y)) --> [X], test2(Y).
    test2(st) --> [bonk].
    
    
    ?-  test(Output, [a, bonk],[]).
    Output = te(a, st).