2
$\begingroup$

Is there any way to programmatically tell whether a given character is a letter or letter-like form (see this reference page for what I mean)?

One idea I had was to use FrontEnd`UndocumentedTestFEParserPacket, to test whether the syntax parser would split the string (i.e. test whether char <> "a" is split), which seems to work. But I am looking for a more provably correct and less undocumented solution if at all possible.

Another idea was to simply check the reference page for a given character/preprocess the full list given above to build a "database" by hand, but it feels like there could be a more straightforward way, hence this question.

$\endgroup$
4
  • $\begingroup$ Yes, there is LetterQ. $\endgroup$ Apr 7, 2018 at 9:40
  • $\begingroup$ @HenrikSchumacher: That doesn't work on some characters though: LetterQ@"\[Bullet]" returns False... $\endgroup$
    – Lukas Lang
    Apr 7, 2018 at 9:43
  • $\begingroup$ Does SyntaxQ plus a check for numerics not work? $\endgroup$
    – b3m2a1
    Apr 9, 2018 at 8:46
  • $\begingroup$ @b3m2a1 No, SyntaxQ@"?" is True unfortunately... $\endgroup$
    – Lukas Lang
    Apr 9, 2018 at 11:54

2 Answers 2

1
$\begingroup$

Hm. Searching for every command involving Letter with *`*Letter*, I found System`Convert`MLStringDataDump`LetterLikeQ. If s is a String, it applies System`Convert`MLStringDataDump`LetterLikeAuxQ to Characters[s] and wraps it with And@@, so it seems to check if a string is entirely composed of letter like characters. Actually, the whole context System`Convert`MLStringDataDump seems to be interesting for you.

$\endgroup$
2
  • 1
    $\begingroup$ Thanks! I should really get into the habit of searching functions like this... I just hope this covers all cases, as the list appears to be mostly hard-coded (or rather the complement is) - but since operators aren't changed to frequently, I don't think I need to worry too much. Anyway, this will definitely be the solution I'll use :) $\endgroup$
    – Lukas Lang
    Apr 7, 2018 at 10:06
  • $\begingroup$ You're welcome. I hope it works out! $\endgroup$ Apr 7, 2018 at 10:17
1
$\begingroup$

Reading the documentation of Symbol, the following should work:

LetterLikeQ[char_] := Module[
  {ret},
  Begin["`llq`"];
  ret = Quiet@Check[Remove@Evaluate@Symbol@char; True, False];
  End[];
  ret
]

Some examples:

LetterLikeQ["a"]
(* True *)

LetterLikeQ["•"]
(* True *)

LetterLikeQ["\[Rule]"]
(* False *)
$\endgroup$
2
  • $\begingroup$ Interesting approach. Should be less hacky with Internal`SymbolNameQ. $\endgroup$ Apr 8, 2018 at 8:34
  • $\begingroup$ Good point - then this solution would also be a one-liner (LetterLikeQ=Internal`SymbolNameQ)... But in that case I'd still prefer your solution, since both are undocumented, and yours is clearer in its intention $\endgroup$
    – Lukas Lang
    Apr 8, 2018 at 9:23

Your Answer

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

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