4
$\begingroup$

I am writing a small Mathematical expression parser and need a list of all Mathematica build-in Heads (system context?) so I can check the type of input.

Such as List,Integer,Times,Plus,Rational,Symbol,Real,Sin,Cos ,Power,Equal, and so on. i.e. those names which return from the command Head[..]. This does not include user level expressions ofcourse. Only system level Heads, obtained from Fresh kernel.

I googled and could not find an answer. May be this is something that can be obtained WolframLanguageData ? But I never used this, and when I looked at it, I did not see how to ask it for this.

$\endgroup$
  • 2
    $\begingroup$ See e.g. here and maybe here. $\endgroup$ – halirutan Jul 6 '17 at 3:56
  • $\begingroup$ Names["System`*"] may help, but it gives you ALL symbols, not just those which are used in a "functional" form, i.e. function[...]. $\endgroup$ – JEM_Mosig Jul 6 '17 at 4:09
  • $\begingroup$ @JEM_Mosig yes I know this command. But I need only names of Heads, not all system level names. Many of the names listed there I do not think are names of Heads. For example $SoundDisplayFunction is listed from the above command. But is this a name of a Head? $\endgroup$ – Nasser Jul 6 '17 at 4:12
3
$\begingroup$

You can generate a list with

Select[
 Names["System`*"],
 Quiet[
   With[{
     dv = ToExpression[#, InputForm, DownValues],
     expr = ToExpression[#, InputForm]
     },
    dv =!= {} && MemberQ[dv, HoldPattern[expr[__]], \[Infinity]]
    ]
   ] &
 ]

I am not entirely sure if this is complete, but it is a starting point.

Alternatively you can hope that the usage string of any function starts with "f[...] ...". There is no guarantee, but it seems to be this way. Then you can use

Table[
 With[{usage = WolframLanguageData[sym, "PlaintextUsage"]},
  If[
   StringQ[usage],
   If[StringMatchQ[usage, sym ~~ "[" ~~ Except["]"] ~~ __], sym, 
    Nothing],
   Nothing
   ]
  ],
 {sym, Names["System`*"]}
 ]

Note, however, that this is very slow! I have done it with the first 200 symbols from Names["System`*"] and it took about 20 seconds.

$\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.