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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:46:12+00:00 2026-05-22T22:46:12+00:00

Example: list:={ Plus[1,1], Times[2,3] } When looking at list , I get {2,6} I

  • 0

Example:

list:={ Plus[1,1], Times[2,3] }

When looking at list, I get

{2,6}

I want to keep them unevaluated (as above) so that list returns

{ Plus[1,1], Times[2,3] }

Later I want to evaluate the functions in list sequence to get

{2,6}

The number of unevaluated functions in list is not known beforehand. Besides Plus, user defined functions like f[x_] may be stored in list

I hope the example is clear.

What is the best way to do this?

  • 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-22T22:46:13+00:00Added an answer on May 22, 2026 at 10:46 pm

    The best way is to store them in Hold, not List, like so:

    In[255]:= f[x_] := x^2;
    lh = Hold[Plus[1, 1], Times[2, 3], f[2]]
    
    Out[256]= Hold[1 + 1, 2 3, f[2]]
    

    In this way, you have full control over them. At some point, you may call ReleaseHold to evaluate them:

    In[258]:= ReleaseHold@lh
    
    Out[258]= Sequence[2, 6, 4]
    

    If you want the results in a list rather than Sequence, you may use just List@@lh instead. If you need to evaluate a specific one, simply use Part to extract it:

    In[261]:= lh[[2]]
    
    Out[261]= 6
    

    If you insist on your construction, here is a way:

    In[263]:= l:={Plus[1,1],Times[2,3],f[2]};
    Hold[l]/.OwnValues[l]
    
    Out[264]= Hold[{1+1,2 3,f[2]}]
    

    EDIT

    In case you have some functions/symbols with UpValues which can evaluate even inside Hold, you may want to use HoldComplete in place of Hold.

    EDIT2

    As pointed by @Mr.Wizard in another answer, sometimes you may find it more convenient to have Hold wrapped around individual items in your sequence. My comment here is that the usefulness of both forms is amplified once we realize that it is very easy to transform one into another and back. The following function will split the sequence inside Hold into a list of held items:

    splitHeldSequence[Hold[seq___], f_: Hold] := List @@ Map[f, Hold[seq]]
    

    for example,

    In[274]:= splitHeldSequence[Hold[1 + 1, 2 + 2]]
    
    Out[274]= {Hold[1 + 1], Hold[2 + 2]}
    

    grouping them back into a single Hold is even easier – just Apply Join:

    In[275]:= Join @@ {Hold[1 + 1], Hold[2 + 2]}
    
    Out[275]= Hold[1 + 1, 2 + 2]
    

    The two different forms are useful in diferrent circumstances. You can easily use things such as Union, Select, Cases on a list of held items without thinking much about evaluation. Once finished, you can combine them back into a single Hold, for example, to feed as unevaluated sequence of arguments to some function.

    EDIT 3

    Per request of @ndroock1, here is a specific example. The setup:

    l = {1, 1, 1, 2, 4, 8, 3, 9, 27} 
    S[n_] := Module[{}, l[[n]] = l[[n]] + 1; l] 
    Z[n_] := Module[{}, l[[n]] = 0; l]
    

    placing functions in Hold:

    In[43]:= held = Hold[Z[1], S[1]]
    
    Out[43]= Hold[Z[1], S[1]]
    

    Here is how the exec function may look:

    exec[n_] := MapAt[Evaluate, held, n]
    

    Now,

    In[46]:= {exec[1], exec[2]}
    
    Out[46]= {Hold[{0, 1, 1, 2, 4, 8, 3, 9, 27}, S[1]],  Hold[Z[1], {1, 1, 1, 2, 4, 8, 3, 9, 27}]}
    

    Note that the original variable held remains unchanged, since we operate on the copy. Note also that the original setup contains mutable state (l), which is not very idiomatic in Mathematica. In particular, the order of evaluations matter:

    In[61]:= Reverse[{exec[2], exec[1]}]
    
    Out[61]= {Hold[{0, 1, 1, 2, 4, 8, 3, 9, 27}, S[1]],  Hold[Z[1], {2, 1, 1, 2, 4, 8, 3, 9, 27}]}
    

    Whether or not this is desired depends on the specific needs, I just wanted to point this out. Also, while the exec above is implemented according to the requested spec, it implicitly depends on a global variable l, which I consider a bad practice.

    An alternative way to store functions suggested by @Mr.Wizard can be achieved e.g. like

    In[63]:= listOfHeld = splitHeldSequence[held]

    Out[63]= {Hold[Z1], Hold[S1]}

    and here

    In[64]:= execAlt[n_] := MapAt[ReleaseHold, listOfHeld, n]
    
    In[70]:= l = {1, 1, 1, 2, 4, 8, 3, 9, 27} ;
    {execAlt[1], execAlt[2]}
    
    Out[71]= {{{0, 1, 1, 2, 4, 8, 3, 9, 27}, Hold[S[1]]}, {Hold[Z[1]], {1, 1, 1, 2, 4, 8, 3, 9, 27}}}
    

    The same comments about mutability and dependence on a global variable go here as well. This last form is also more suited to query the function type:

    getType[n_, lh_] := lh[[n]] /. {Hold[_Z] :> zType, Hold[_S] :> sType, _ :> unknownType}
    

    for example:

    In[172]:= getType[#, listOfHeld] & /@ {1, 2}
    
    Out[172]= {zType, sType}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to keep a collection-object (List in example) in a Key/Value
I can't get icon in properManner, I use PackageManager : Code example ::List<PackageInfo> applications
Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
In my admin view I would like several sub directories for example: admin/users/list.aspx I
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I have a list of objects (for the sake of example, let's say 5).
Following this example, I can list all elements into a pdf file import pyPdf
For example I have such query: Query q = sess.createQuery(from Cat cat); List cats
As an example, lets say I wanted to list the frequency of each letter
For example: from datetime import <c-x><c-o>{list of modules inside datetime package}

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.