15
$\begingroup$

Sometimes you need to check for the type of an expression. You use Head[data] to get answers.

Some data:

data = {7, 1.4, Red, "hp", hps};
Head /@ data
(*{Integer, Real, RGBColor, String, Symbol}*)

Is there summary of what kind of answers Head[] can give?

$\endgroup$
2
  • $\begingroup$ I see that you have not Accepted my answer. If you find it lacking please tell me in which way, so that I may improve it. If it is not lacking please consider Accepting it. $\endgroup$
    – Mr.Wizard
    Apr 4, 2013 at 23:33
  • $\begingroup$ see here for a good summary :) $\endgroup$
    – user1066
    Jul 15, 2017 at 17:59

1 Answer 1

24
$\begingroup$

Head can return any head. There is no predefined list.

expr = myArbitraryHead[1, 2, 3];

Head[expr]
myArbitraryHead

A head does not even need to be a Symbol:

expr2 = (2 Pi)[x, y, z];

Head[expr2]
2 π

Most heads are shown explicitly in the FullForm of the expression:

FullForm[{"a" + "b", 1/3}]

Head /@ {"a" + "b", 1/3}
List[Plus["a", "b"], Rational[1, 3]]

{Plus, Rational}

Some heads are implicit, such as in the atomic objects String, Integer, and Symbol:

FullForm[{"a", 1, Pi}]

Head /@ {"a", 1, Pi}
List["a", 1, Pi]

{String, Integer, Symbol}

As a side note the head of an expression is also returned when you ask for Part zero:

3[[0]]
Integer

Because Part returns sequences wrapped in the original head of the expression you get a strange result if extracting {0}:

3[[{0}]]
Integer[Integer]
$\endgroup$
7
  • 5
    $\begingroup$ +1 Also, for atomic data types (Integer, Rational, etc. and some more, e.g. Graphs) it returns the type rather than the head. Though it is more of a philosophical question what the differences is between type and head in Mathematica. $\endgroup$ Apr 1, 2013 at 9:26
  • $\begingroup$ +1 Mr.Wizard introducing terminology is nice. $\endgroup$ Apr 1, 2013 at 13:36
  • 2
    $\begingroup$ I voted for your answer, but I disagree with the hidden head part. We should not use StandardForm (or, rather, you probably meant particular formatting which visually looks as if we had a division) for determination of heads and other structural components. Let's apply Occam's razor principle and not multiply entities unnecessarily. As to implicit heads, this is probably ok, but even here I am not sure - those are really the heads of certain atomic types, and there are just very few of those - basically they exist because the syntax allows for string and numeric literals. $\endgroup$ Apr 1, 2013 at 15:50
  • $\begingroup$ @Leonid I think I understand your point about "hidden" heads, and I agree that was a poor choice (and will be edited). As for implicit heads how would you state it? $\endgroup$
    – Mr.Wizard
    Apr 1, 2013 at 21:17
  • $\begingroup$ I'd just say that atomic types represented as string or numeric literals also have heads (as any expression in Mathematica), but these heads are assigned to them internally and don't follow from their FullForm representation. This refers to Integer, Real, String and Symbol (not sure if I forgot some). Something like that. $\endgroup$ Apr 2, 2013 at 7:08

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.