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

  • SEARCH
  • Home
  • 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 4230954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:55:06+00:00 2026-05-21T01:55:06+00:00

I recently finished a project in which I was generating lists of strings, and

  • 0

I recently finished a project in which I was generating lists of strings, and I was wondering about the best way to do this.

The string generation was context sensitive to determine if it was acceptable(it was a sequence of plays in a game, so you had to know what the last play was)

The way I did it was with a function that was passed the context parameter and the term, and if it was acceptable it recursively continued, if it wasn’t it terminated(since no further string could be acceptable.) The function also received a “length” parameter to make sure it terminated eventually

basically this is generating every possible string accepted by a language(of a certain length).

Now, I got this to work, even fairly well and cleanly, but I was wondering if there were a better way to do this. Specifically, would a “state machine” monad work well in generating a context sensitive grammar? or something similar at least? The problem seems to simple to want to fire up something like parsec, are there other structures that are effective in manipulating languages?

Any thoughts would be appreciated.

  • 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-21T01:55:07+00:00Added an answer on May 21, 2026 at 1:55 am

    I thought this problem looked interesting, so I tried a few different ways of implementing it. The code below is the approach the seemed most promising. I think it solves the problem as described, although I was unsure of some details.

    Basically it allows a form of context sensitive grammars, but only a quite simple form where each production can only depend on the previous symbol. The code below builds up some combinators that allow the productions to be coded very directly as “generators”, and that take care of the length limit behind the scene.

    type sym = Xa | Xb | Xc          // The terminal symbols 
    type word = sym list             // A string of terminals
    
    type gen = int -> sym list seq   // Generate words up to the given length
    
    /// Passes the previous symbol to a generator, after checking the length.
    let (+>) : sym  -> (sym -> gen) -> gen = 
        fun x g l -> if l<=0 then Seq.empty 
                             else seq { for xs in g x (l-1) -> x :: xs }
    
    let nil _ = seq { yield [] }                    // Generate just the empty word            
    let (|||) xs ys l = Seq.append (xs l) (ys l)    // Union of two generators
    let notAccepted _ = Seq.empty                   // Don't generate anything
    
    let tryAll g = Xa +> g ||| Xb +> g ||| Xc +> g  // Run g starting with any sym
    
    // Generators for non-terminals.  Each takes the previous symbol as an argument,
    // and generates a (possibly empty) sequence of lists of symbols.
    let rec gR = function Xa ->  Xb +> gS ||| Xc +> gR  ||| nil  
                        | Xc ->  Xb +> gS                        | _ -> notAccepted
        and gS = function Xb ->  Xa +> gR                        | _ -> notAccepted
    
    
    let genTop = tryAll gR  // The top level generator begins with gR with any sym
    
    let words = genTop 4    // Generate words up to length 4
    // val words : seq<sym list> 
    //           = seq [[Xa; Xb; Xa]; [Xa; Xc; Xb; Xa]; [Xa]; [Xc; Xb; Xa]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just looked back through the project that nearly finished recently and found a
i recently finished one project..now when i create a new navigation project in xcode
At my work we recently finished the system architecture for a control application which
I recently finished my project in xcode and decided to test it on my
I am new to F#, about two months, and I recently finished the FParsec
I recently read this article Safe Thread Synchronization as I was curious about the
I recently finished a project using TDD and I found the process to be
I recently finished an application using just standard grails way (GORM-domain classes, etc.), but
I did a quiz page for our computer project and I've recently finished it.I
I'm very new to Ruby. I've recently finished writing a simple script, which uses

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.