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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:41:48+00:00 2026-05-30T11:41:48+00:00

I’ve been working on adding a counter to variables in a given expression and

  • 0

I’ve been working on adding a counter to variables in a given expression and have the following test case:

prop_add1 = add (And (Var "a") (And (Var "b") (Var "a"))) ==
And (Var "a1") (And (Var "b2") (Var "a1"))

I have been using Pattern Matching and Recursion to try and find a solution. Although I have come a little stuck, I have tried adding the original variables to a list and then using the list to determine the variable name to be output but I couldn’t figure out how to correctly implement it and my output just adds a 1 to the end of all variables.

I’m wondering is there a better/easier solution to this?

My Attempt so far:

add :: Expr -> Expr
add T = T
add (Var x) = Var (x ++ show (check2(check x)))
add (And e1 e2) = And (add e1) (add e2)
add (Not e1) = Not (add e1)

check :: Variable -> [Variable]
check p = [p]

check2 :: [Variable] -> Int
check2 p = length (union p p)
  • 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-30T11:41:49+00:00Added an answer on May 30, 2026 at 11:41 am

    You need to keep an environment mapping variable names to numbers. Something like

    add :: Expr -> Expr
    add expr = fst $ addWithEnv emptyEnv expr
      where
        emptyEnv = []
        addWithEnv env (And e1 e2)
            = case addWithEnv env e1 of
                (e1', env') ->
                  case addWithEnv env' e2 of
                    (e2', env'') -> (And e1' e2', env'')
        addWithEnv env (Var name)
            = case lookup name env of
                Just k -> -- stopping here,it's homework
    

    I hope I’ve left enough for you to fill in.

    Update:

    In your attempt, you don’t keep track of which variables you have already seen, so every variable seems to be the first, and every time a ‘1’ is appended. Numbering items is a stateful computation, you must have a record of which variables have been seen so far to assign previously seen variables the old number and know which number to assign the next not-yet-seen variable. So you must carry that record around in the worker. If you already know about the Monad class and how to use that, you can implicitly carry it around using the State monad, otherwise you have to carry it around explicitly. Then add becomes a wrapper that calls the worker with an initially empty state (before the numbering/renaming starts, no variable has yet been seen). The worker then looks at the subexpressions of the given expression (if any) and renames variables and updates the state when a new variable is encountered.

    So in the sketch above, we have

    addWithEnv :: [(String,Int)] -> Expr -> (Expr, [(String,Int)])
    

    since we cannot mutate the state, we have to return the new state along with the renamed expression. Now you have to define what the result shall be for each type of expression,

    addWithEnv env T = ??
    addWithEnv env (Var name) = ??
    addWithEnv env (Add e1 e2) = ??
    addWithEnv env (Not e) = ??
    

    The T case of course does no renaming and doesn’t update the environment. A Var has either been seen before, in which case the environment remains unchanged, or not, in which case it is added to the environment. A Not e has the same influence on the environment as e, and an And e1 e2 has the combined effects of first e1 then e2 on the environment.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
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
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.