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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:04:03+00:00 2026-06-10T23:04:03+00:00

I’m writing a programming language interpreter. I have need of the right code idiom

  • 0

I’m writing a programming language interpreter.

I have need of the right code idiom to both evaluate a sequence of expressions to get a sequence of their values, and propagate state from one evaluator to the next to the next as the evaluations take place. I’d like a functional programming idiom for this.

It’s not a fold because the results come out like a map. It’s not a map because of the state prop across.

What I have is this code which I’m using to try to figure this out. Bear with a few lines of test rig first:

// test rig
class MonadLearning extends JUnit3Suite {

  val d = List("1", "2", "3") // some expressions to evaluate. 

  type ResType = Int 
  case class State(i : ResType) // trivial state for experiment purposes
  val initialState = State(0)

// my stub/dummy "eval" function...obviously the real one will be...real.
  def computeResultAndNewState(s : String, st : State) : (ResType, State) = {
    val State(i) = st
    val res = s.toInt + i
    val newStateInt = i + 1
    (res, State(newStateInt))
  }

My current solution. Uses a var which is updated as the body of the map is evaluated:

  def testTheVarWay() {
    var state = initialState
    val r = d.map {
      s =>
        {
          val (result, newState) = computeResultAndNewState(s, state)
          state = newState
          result
        }
    }
    println(r)
    println(state)
  }

I have what I consider unacceptable solutions using foldLeft which does what I call “bag it as you fold” idiom:

def testTheFoldWay() {

// This startFold thing, requires explicit type. That alone makes it muddy.
val startFold : (List[ResType], State) = (Nil, initialState)
val (r, state) = d.foldLeft(startFold) {
  case ((tail, st), s) => {
    val (r, ns) = computeResultAndNewState(s, st)
    (tail :+ r, ns) // we want a constant-time append here, not O(N). Or could Cons on front and reverse later
  }
}

println(r)
println(state)

}

I also have a couple of recursive variations (which are obvious, but also not clear or well motivated), one using streams which is almost tolerable:

def testTheStreamsWay() {
  lazy val states = initialState #:: resultStates // there are states
  lazy val args = d.toStream // there are arguments
  lazy val argPairs = args zip states // put them together
  lazy val resPairs : Stream[(ResType, State)] = argPairs.map{ case (d1, s1) => computeResultAndNewState(d1, s1) } // map across them
  lazy val (results , resultStates) = myUnzip(resPairs)// Note .unzip causes infinite loop. Had to write my own.

  lazy val r = results.toList
  lazy val finalState = resultStates.last

  println(r)
  println(finalState)
}

But, I can’t figure out anything as compact or clear as the original ‘var’ solution above, which I’m willing to live with, but I think somebody who eats/drinks/sleeps monad idioms is going to just say … use this… (Hopefully!)

  • 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-06-10T23:04:05+00:00Added an answer on June 10, 2026 at 11:04 pm

    With the map-with-accumulator combinator (the easy way)

    The higher-order function you want is mapAccumL. It’s in Haskell’s standard library, but for Scala you’ll have to use something like Scalaz.

    First the imports (note that I’m using Scalaz 7 here; for previous versions you’d import Scalaz._):

    import scalaz._, syntax.std.list._
    

    And then it’s a one-liner:

    scala> d.mapAccumLeft(initialState, computeResultAndNewState)
    res1: (State, List[ResType]) = (State(3),List(1, 3, 5))
    

    Note that I’ve had to reverse the order of your evaluator’s arguments and the return value tuple to match the signatures expected by mapAccumLeft (state first in both cases).

    With the state monad (the slightly less easy way)

    As Petr Pudlák points out in another answer, you can also use the state monad to solve this problem. Scalaz actually provides a number of facilities that make working with the state monad much easier than the version in his answer suggests, and they won’t fit in a comment, so I’m adding them here.

    First of all, Scalaz does provide a mapM—it’s just called traverse (which is a little more general, as Petr Pudlák notes in his comment). So assuming we’ve got the following (I’m using Scalaz 7 again here):

    import scalaz._, Scalaz._
    
    type ResType = Int
    case class Container(i: ResType)
    
    val initial = Container(0)
    val d = List("1", "2", "3")
    
    def compute(s: String): State[Container, ResType] = State {
      case Container(i) => (Container(i + 1), s.toInt + i)
    }
    

    We can write this:

    d.traverse[({type L[X] = State[Container, X]})#L, ResType](compute).run(initial)
    

    If you don’t like the ugly type lambda, you can get rid of it like this:

    type ContainerState[X] = State[Container, X]
    
    d.traverse[ContainerState, ResType](compute).run(initial)
    

    But it gets even better! Scalaz 7 gives you a version of traverse that’s specialized for the state monad:

    scala> d.traverseS(compute).run(initial)
    res2: (Container, List[ResType]) = (Container(3),List(1, 3, 5))
    

    And as if that wasn’t enough, there’s even a version with the run built in:

    scala> d.runTraverseS(initial)(compute)
    res3: (Container, List[ResType]) = (Container(3),List(1, 3, 5))
    

    Still not as nice as the mapAccumLeft version, in my opinion, but pretty clean.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

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.