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).
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).
As you said, st is followed by "a bunch of nonsense" because of _X (basically, _G6369 is the internal 'name' of the variable and since the variable remains uninstantiated prolog displays it; try print(X), X=3, print(X).
Anyway, you can simply remove (_X) since you can have anything you want as an argument:
test(te(X,Y)) --> [X], test2(Y).
test2(st) --> [bonk].
Of course, if you don't actually have bonk's in your input and you simply want to add a st at the end you can simplify it even more:
test(te(X,st)) --> [X].
Or if you have bonk's:
test(te(X,st)) --> [X,bonk].
Finally, it is generally suggested to use phrase/3 or phrase/2 instead of adding the arguments manually.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With