2
$\begingroup$

We know:

Map[Head, {a, a^2, a^3}]
(* {Symbol, Power, Power} *)

and Map[Head, {2, 2^2, 2^3}] all of them will be known as Integers.

But how can we get the result of

Map[Head, {2, 2^2, 2^3}]` 
{Integer, Power, Power} 

I mean how Mathematica recognize a value is a power of two others?

$\endgroup$
4
  • 2
    $\begingroup$ #[[1, 0]] & /@ {Hold[2], Hold[2^2], Hold[2^3]} gives Integer, Power, Power $\endgroup$
    – LLlAMnYP
    Feb 19, 2016 at 9:11
  • 3
    $\begingroup$ Just for the record, after 2^2 evaluates and becomes 4, it is not a Power. $\endgroup$
    – LLlAMnYP
    Feb 19, 2016 at 9:13
  • 3
    $\begingroup$ Alternatively SetAttributes[f, HoldAll]; f[x_]:=Head@Unevaluated@x; Map[f, Unevaluated@{2, 2^2, 2^3}] $\endgroup$
    – LLlAMnYP
    Feb 19, 2016 at 9:17
  • 1
    $\begingroup$ Have a look at PrimePowerQ[]. $\endgroup$ Feb 19, 2016 at 11:27

1 Answer 1

1
$\begingroup$

You can use solutions from How to map Unevaluated over a list to prepare your list to be mapped with Head. E.g.:

Map[Head, Unevaluated /@ Unevaluated @ {1, 2^2, 2^3}]
{Integer, Power, Power}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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