Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DCG problem

Status
Not open for further replies.

confusedstudent1

Programmer
Joined
Dec 3, 2010
Messages
3
I have to write a DCG that generates strings from the alphabet a E {0,1,2} the strings have to have the form nn. This means queries like ?- s([0,2,1,0,2,1],[]). and s([1,1,2,0,1,1,2,0],[]). would return true.

This is the code I have so far. I'm so close. Can someone help me further?


s --> s(X), s(X).
s --> [].

s(0) --> zero, s(X).
s(1) --> one, s(X).
s(2) --> two, s(X).
s(3) --> [].

zero --> [0].
one --> [1].
two --> [2].


Many thanks ;)
 
Code:
s --> [].
s --> sx, s.

sx --> [0].
sx --> [1].
sx --> [2].
 
You are very close to the solution !

Just look at these rules
s --> s(X), s(X).
and
s(0) --> zero, s(X).

In the first rule, what is the type of X, an atom or a list ?
In the second rule you use an atom 0, and you introduce X in the body of the rule. You don't have a lot of things to change/add.
 
No kahleen the production must have the form nn, you accept [0,1,2] for example.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top