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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:23:53+00:00 2026-05-24T04:23:53+00:00

A light-weight question for the experts. I can’t seem to figure the correct syntax

  • 0

A light-weight question for the experts. I can’t seem to figure the correct syntax to this replacement. I have this list

Clear[a, b, c, d]
polesList = {{3, {a, b}}, {5, {c, d}}};

It is of the form of a list with sublists each have the form {order,{x,y}} and I want to generate a new list of this form (x+y)^order

Currently this is what I do, which works:

((#[[2, 1]] + #[[2, 2]])^#[[1]]) & /@ polesList

(* ----->   {(a + b)^3, (c + d)^5}  *)  

But I have been trying to learn to use ReplaceAll as it is more clear to me than pure functions, since I can see the pattern better, like this:

Clear[a, b, c, d, n]
polesList = {{3, {a, b}}, {5, {c, d}}};
ReplaceAll[polesList, {n_, {x_, y_}} :> (x + y)^n]   (*I thought this will work*)

I get strange result, which is

{(5 + c)^3, {(5 + d)^a, (5 + d)^b}}

What is the correct syntax to do this replacement using ReplaceAll instead of the pure function method?

Thanks

Update:

I find that using Replace, instead of ReplaceAll works, but need to say {1} at the end:

Clear[a, b, c, d, n]
polesList = {{3, {a, b}}, {5, {c, d}}};
Replace[polesList, {n_, {x_, y_}} :> (x + y)^n, {1}]

which gives

{(a + b)^3, (c + d)^5}

But ReplaceAll does not take {1} at the end. I am more confused now which to use 🙂

  • 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-24T04:23:54+00:00Added an answer on May 24, 2026 at 4:23 am

    The problem is that ReplaceAll inspects all levels of the expression when looking for replacements. The entire expression matches the pattern {n_, {x_, y_}} where:

    n matches {3, {a, b}}

    x matches 5

    y matches {c, d}

    So you end up with (5 + {c , d}) ^ {3, {a, b}} which evaluates to the result you see.

    There are a few ways to fix this. First, you can change the pattern so that it does not match the outermost list. For example, if the n values are always integers you could use:

    ReplaceAll[polesList, {n_Integer, {x_, y_}} :> (x + y)^n]
    

    Or, you could use Replace instead of ReplaceAll, and restrict the pattern matching the first level only:

    Replace[polesList, {n_, {x_, y_}} :> (x + y)^n, {1}]
    

    I find that applying replacement rules to the first level of a list is very common. It so happens that Cases, by default, only operates on that level. So I find myself frequently using Cases for level one replacements when I know that all elements will match the pattern:

    Cases[polesList, {n_, {x_, y_}} :> (x + y)^n]
    

    This last expression is how I would probably write the desired replacement. Keep in mind, though, that if all elements do not match the pattern, then the Cases approach will drop the mismatches from the result.

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

Sidebar

Related Questions

When I asked this previously I should have mentioned that it's particularly a light-weight
Although somewhat related to this question , I have what I think is a
Background for understanding the question I'm currently designing a simple, light-weight (application-specific) message-based API
While looking for a light-weight Scala development environment, I came upon an Scala edit
I’m starting a project where I need to implement a light-weight interpreter. The interpreter
Whenever I read about Swing they say they are light weight components. So I
We recently changed some of our system requirements on a light weight application (it
If this question seems common to you, I apologise, I did a quick search
Guys / Gals i have a real basic Team Foundation Server 2010 question. For
I think this question may end up being a bit subjective so I'm marking

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.