2

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.

kglr
  • 394,356
  • 18
  • 477
  • 896
StarBucK
  • 2,164
  • 1
  • 10
  • 33
  • 1
    try Length[bb[[1]]] to get 4. Check Head[bb] and FullForm[bb] to see why you are getting 1. – kglr Jul 09 '17 at 18:14
  • The head is a HoldForm and indeed, Length[bb[[1]]=4. But why in the result the HoldForm disappears ? – StarBucK Jul 09 '17 at 18:18
  • Strabuck, that's what HoldForm does. HoldForm >> Details: HoldForm allows you to see the output form of an expression without evaluating the expression. – kglr Jul 09 '17 at 18:21
  • @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 ? – StarBucK Jul 09 '17 at 18:30
  • 1
    Starbuck, perhaps, Inactivate is what you need. – kglr Jul 09 '17 at 18:47

2 Answers2

8

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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
6

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

kglr
  • 394,356
  • 18
  • 477
  • 896