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 898733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:05:45+00:00 2026-05-15T15:05:45+00:00

Some background first. I am currently learning some stuff about monadic parser combinators. While

  • 0

Some background first. I am currently learning some stuff about monadic parser combinators. While I tried to transfer the ‘chainl1’ function from this paper (p. 16-17), I came up with this solution:

let chainl1 p op = parser {
  let! x = p
  let rec chainl1' (acc : 'a) : Parser<'a> =
      let p' = parser {
          let! f = op
          let! y = p
          return! chainl1' (f acc y)
          }
      p' <|> succeed acc
  return! chainl1' x
}

I tested the function with some large input and got a StackOverflowException. Now I am wondering, is it posible to rewrite a recursive function, that uses some computation expression, in a way so it is using tail recursion?

When I expand the computation expression, I can not see how it would be generally possible.

let chainl1 p op =
    let b = parser
    b.Bind(p, (fun x ->
    let rec chainl1' (acc : 'a) : Parser<'a> =
        let p' =
            let b = parser
            b.Bind(op, (fun f ->
            b.Bind(p, (fun y ->
            b.ReturnFrom(chainl1' (f acc y))))))
        p' <|> succeed acc
    b.ReturnFrom(chainl1' x)))
  • 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-15T15:05:46+00:00Added an answer on May 15, 2026 at 3:05 pm

    In your code, the following function isn’t tail-recursive, because – in every iteration – it makes a choice between either p' or succeed:

    // Renamed a few symbols to avoid breaking SO code formatter
    let rec chainl1Util (acc : 'a) : Parser<'a> = 
      let pOp = parser { 
        let! f = op 
        let! y = p 
        return! chainl1Util (f acc y) } 
      // This is done 'after' the call using 'return!', which means 
      // that the 'cahinl1Util' function isn't really tail-recursive!
      pOp <|> succeed acc 
    

    Depending on your implementation of parser combinators, the following rewrite could work (I’m not an expert here, but it may be worth trying this):

    let rec chainl1Util (acc : 'a) : Parser<'a> = 
      // Succeeds always returning the accumulated value (?)
      let pSuc = parser {
        let! r = succeed acc
        return Choice1Of2(r) }
      // Parses the next operator (if it is available)
      let pOp = parser {
        let! f = op
        return Choice2Of2(f) }
    
      // The main parsing code is tail-recursive now...
      parser { 
        // We can continue using one of the previous two options
        let! cont = pOp <|> pSuc 
        match cont with
        // In case of 'succeed acc', we call this branch and finish...
        | Choice1Of2(r) -> return r
        // In case of 'op', we need to read the next sub-expression..
        | Choice2Of2(f) ->
            let! y = p 
            // ..and then continue (this is tail-call now, because there are
            // no operations left - e.g. this isn't used as a parameter to <|>)
            return! chainl1Util (f acc y) } 
    

    In general, the pattern for writing tail-recursive functions inside computation expressions works. Something like this will work (for computation expressions that are implemented in a way that allows tail-recursion):

    let rec foo(arg) = id { 
      // some computation here
      return! foo(expr) }
    

    As you can check, the new version matches this pattern, but the original one did not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you need is a static variable and a mean… May 16, 2026 at 8:52 am
  • Editorial Team
    Editorial Team added an answer You push values to the view from the controller by… May 16, 2026 at 8:52 am
  • Editorial Team
    Editorial Team added an answer The views folder should be used for aspx/ascx views. You'll… May 16, 2026 at 8:52 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

Related Questions

I'm currently polling my CFReadStream for new data with CFReadStreamHasBytesAvailable . (First, some background:
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
First, some background: I'm developing a web application using Python. All of my (text)
Here's some background info. I have three MySQL tables (all InnoDB). The first table
I'm currently kicking off a background thread to do some REST queries in my
Some background: I'm a jack-of-all trades, one of which is programming. I learned VB6
I am learning Python (I have a C/C++ background). I need to write something
a bit of background first... I am setting up a versioning numbering system for
I'll give you a little bit of background first as to why I'm asking
Here's the background info first. ASP.NET 2.0 Web Site with AJAX Extensions 1.0. I

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.