prolog - Right linear context free grammar with even difference of 0's and 1's -
i'm trying write right linear context free grammar, in difference between numbers of 0's , 1's should even. example:
010001 = 4 - 2 = 2 (even) i had similar problem. maybe help! i'm trying write on prolog. did other 10 exercises difficult me. ideas on how it?
my code
s --> []. s --> [1],a. s --> [0],b. --> [1],s. --> [0],c. b --> [1],c. b --> [0],s. c --> []. c --> [1],b. c --> [0],a. it's work many cases i'm not sure if well.
the problem can simplified little math.
let a number of 0, b - number of 1, n - length of word. want abs(a - b) even.
do math:
a + b = n b = n - a - b = - n + = 2*a - n 2*a even, abs(a - b) iff n even.
so task check if length of word even.
solution:
s --> []. s --> digit, digit, s. digit --> [0]. digit --> [1].
Comments
Post a Comment