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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:07:53+00:00 2026-06-03T01:07:53+00:00

Here is an Expr class from the stairway book. abstract class Expr case class

  • 0

Here is an Expr class from the stairway book.

abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class UnOp(operator: String, arg: Expr) extends Expr
case class BinOp(operator: String, left: Expr, right: Expr) extends Expr

Now, I want a function to rename a variable in a expression. Here is my first attempt.

def renameVar(expr: Expr, varName: String, newName: String): Expr = expr match {
    case Var(name) if name == varName => Var(newName)
    case Number(_) => expr
    case UnOp(operator, arg) => UnOp(operator, renameVar(arg, varName, newName))
    case BinOp(operator, left, right) => BinOp(operator, renameVar(left, varName, newName), renameVar(right, varName, newName))
}

val anExpr = BinOp("+", Number(1), Var("x"))
val anExpr2 = renameVar(anExpr, "x", "y")

This works but is tedious (the actual class I am working with has several case subclasses). Also, I might need several similar transformations. Is there a better alternative (possibly using higher order functions)?

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

    So your version of renameVar has to know about two separate things: it has to know how to recurse the tree and it has to know how to rename a variable.

    One solution might be to separate these two concerns. You could use the visitor design pattern to give each class control over how it does recursion; the visit method is only concerned with how to traverse the tree. As it traverses, it can pass through a function that handles the actual work (renaming a variable in your case).

    Here is a simple implementation that passes a transformation function (that operates on Expr and returns an Expr). The fact that it uses a PartialFunction allows you to pattern-match the expression in the tree to operate on. Any expressions not covered by the cases just fall back to the normal recursion (as specified by doVisit).

    Depending on the variety of different tasks, you might need to have a more complex visit method. But this should give you an idea of the direction:

    // Class Hierarchy
    abstract class Expr {
      def visit(f: PartialFunction[Expr, Expr]): Expr = if (f.isDefinedAt(this)) f(this) else doVisit(f)
      protected def doVisit(f: PartialFunction[Expr, Expr]): Expr
    }
    case class Var(name: String) extends Expr {
      protected def doVisit(f: PartialFunction[Expr, Expr]) = this
    }
    case class Number(num: Double) extends Expr {
      protected def doVisit(f: PartialFunction[Expr, Expr]) = this
    }
    case class UnOp(operator: String, arg: Expr) extends Expr {
      protected def doVisit(f: PartialFunction[Expr, Expr]) = UnOp(operator, arg.visit(f))
    }
    case class BinOp(operator: String, left: Expr, right: Expr) extends Expr {
      protected def doVisit(f: PartialFunction[Expr, Expr]) = BinOp(operator, left.visit(f), right.visit(f))
    }
    
    // Transformation Functions
    def renameVar(expr: Expr, varName: String, newName: String): Expr = {
      expr.visit { case Var(`varName`) => Var(newName) }
    }
    

    Now you can introduce a new class, like TernaryOp(String, Expr, Expr, Expr), define its doVisit method in a similar way, and it will work without you having to modify renameVar (or any other transformation functions like renameVar).

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

Sidebar

Related Questions

The language is Ruby, here is my irb session expr = /\Aselect from (\D+)(?:
Here is a complete example. I want to forbid using A::set from objects casted
Here's a Clone() implementation for my class: MyClass^ Clone(){ return gcnew MyClass(this->member1, this->member2); }
According to the documentation AbsoluteOptions[expr,name] gives the absolute setting for the option name. AbsoluteOptions
Here is the code for a simple arithmetic expression parser I wrote: class ArithmeticExpressionParser<T>
Why does PEP-343 use type() in this situation here? mgr = (EXPR) exit =
Here I'm selecting the node: $ xmlstarlet sel -t -c /configuration/property[name='http.agent.name']/value conf/nutch-default.xml <value/> This
Ok so here is the example query: SELECT DISTINCT id, amount FROM tbl_recurring_payment AS
I currently have a base class Expr and some derivatives like AddExpr , MultExpr
I have followed a number of questions here that asks about how to convert

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.