2
$\begingroup$

Here is the code :

bb = HoldForm[{aa[1], bbb[2], cc[3], dd[4]}]

it returns :

{aa[1], bbb[2], cc[3], dd[4]}

Which is what I expect.

But when I do :

Length[bb]

It returns

1

Why ?? Why doesn't it return 4?

The aa, bbb, cc, dd are functions that I want to keep in an unevaluated form.

$\endgroup$
  • 1
    $\begingroup$ try Length[bb[[1]]] to get 4. Check Head[bb] and FullForm[bb] to see why you are getting 1. $\endgroup$ – kglr Jul 9 '17 at 18:14
  • $\begingroup$ The head is a HoldForm and indeed, Length[bb[[1]]=4. But why in the result the HoldForm disappears ? $\endgroup$ – StarBucK Jul 9 '17 at 18:18
  • $\begingroup$ Strabuck, that's what HoldForm does. HoldForm >> Details: HoldForm allows you to see the output form of an expression without evaluating the expression. $\endgroup$ – kglr Jul 9 '17 at 18:21
  • $\begingroup$ @kglr I edited my message. If I understand it is just a matter of visualisation ? If I want to "really" unevaluate the things inside because I want to keep it in a "factorised" form before sending it to a function it will not work ? $\endgroup$ – StarBucK Jul 9 '17 at 18:30
  • 1
    $\begingroup$ Starbuck, perhaps, Inactivate is what you need. $\endgroup$ – kglr Jul 9 '17 at 18:47
6
$\begingroup$

Regarding "The aa, bbb,cc,dd are functions that I want to keep in an unevaluated form":

If you have version 10 or a newer version, Inactivate is useful for this purpose:

aa = # + 1 &;
bbb = #^2 &;
bb2 = Inactivate[{aa[1], bbb[2], cc[3], dd[4]}]

enter image description here

Length @ bb2

4

TreeForm @ bb2

enter image description here

bb2[[2]]

enter image description here

bb2[[2]]//Activate

4

$\endgroup$
  • $\begingroup$ Yeaaah exactly what I wanted to do. Thanks a lot ! $\endgroup$ – StarBucK Jul 10 '17 at 11:23
8
$\begingroup$

The expression bb has only one leaf, a List:

bb = HoldForm[{aa[1], bbb[2], cc[3], dd[4]}];

bb // TreeForm

enter image description here

If you want to hold separate elements that will be individually manipulated perhaps:

newbb = Thread[bb];

newbb // TreeForm

enter image description here

The Length of newbb is 4.

Possibly of interest:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.