Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 843657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:09:47+00:00 2026-05-15T06:09:47+00:00

I have been progressing through Learn Prolog Now! as self-study and am now learning

  • 0

I have been progressing through Learn Prolog Now! as self-study and am now learning about Definite Clause Grammars. I am having some difficulty with one of the Practical Session’s tasks. The task reads:

The formal language anb2mc2mdn consists of all strings of the following form: an unbroken block of as followed by an unbroken block of bs followed by an unbroken block of cs followed by an unbroken block of ds, such that the a and d blocks are exactly the same length, and the c and d blocks are also exactly the same length and furthermore consist of an even number of cs and ds respectively. For example, ε, abbccd, and aaabbbbccccddd all belong to anb2mc2mdn. Write a DCG that generates this language.

I am able to write rules that generate andn, b2mc2m, and even anb2m and c2mdn… but I can’t seem to join all these rules into anb2mc2mdn. The following are my rules that can generate andn and b2mc2m.

s1 --> [].
s1 --> a,s1,d.
a --> [a].
d --> [d].

s2 --> [].
s2 --> c,c,s2,d,d.
c --> [c].
d --> [d].

Is anb2mc2mdn really a CFG, and is it possible to write a DCG using only what was taught in the lesson (no additional arguments or code, etc)? If so, can anyone offer me some guidance how I can join these so that I can solve the given task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:09:48+00:00Added an answer on May 15, 2026 at 6:09 am

    @Timothy, your answer works, but it generates duplicates:

    ?- length(S,_), s(S,[]).
    S = [] ;
    S = [a, d] ;
    S = [a, d] ;            % XXX
    S = [b, b, c, c] ;
    S = [a, a, d, d] ;
    S = [a, a, d, d] ;      % XXX
    

    This can be fixed by removing one clause, leaving the DCG:

    s --> x.
    s --> a,s,d.
    
    x --> [].
    x --> b,b,x,c,c.
    
    % a, b, c, d the same
    

    This generates:

    ?- length(S,_), s(S,[]).
    S = [] ;
    S = [a, d] ;
    S = [b, b, c, c] ;
    S = [a, a, d, d] ;
    S = [a, b, b, c, c, d] ;
    S = [a, a, a, d, d, d] ;
    S = [b, b, b, b, c, c, c, c] ;
    S = [a, a, b, b, c, c, d, d] ;
    S = [a, a, a, a, d, d, d, d] ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 424k
  • Answers 424k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If model_obj were an instance of the Model class that… May 15, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer I suspect the browser has precedent over the Silverlight plugin.… May 15, 2026 at 11:56 am
  • Editorial Team
    Editorial Team added an answer void f(string &param); string myString = "something"; f(myString) Is that… May 15, 2026 at 11:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.