Comments on a suggested
SyntaxQ emulator
|
D. Vasholz
Comments on a suggested
SyntaxQ emulator
|
D. Vasholz
SyntaxQ emulator
|
D. Vasholz
Introduction
Introduction
As part of this Wolfram Community thread, Eric Rembley suggested the function IsExpression as a rough approximation to a SyntaxQ emulator. He used an approach in which my expertise is presently very limited. However no such expertise is required to directly compare the two functions.
Definition of IsExpression
Definition of IsExpression
The following has been suggested by Eric Rembley:
In[]:=
ReduceExpressionStep[token_String][exp_String]:=With[{NumberPattern=RegularExpression["(\\d+\\.\\d+)|(\\d+\\.)|(\\.\\d+)|(\\d+)"],StringPattern=RegularExpression["\"[^\"]\""],SymbolPattern=RegularExpression["[[:alpha:]]+"],HeadedPattern="$[]",SequencePattern=RegularExpression["\\[(\\$,)*\\$\\]"]},StringReplace[StringDelete[exp,Whitespace],{NumberPattern->token,StringPattern->token,SymbolPattern->token,HeadedPattern->token,SequencePattern->""}]];ReduceExpression[token_String][exp_String]:=FixedPoint[ReduceExpressionStep[token],exp];IsExpression[exp_String]:="$"==ReduceExpression["$"][exp]
IsExpression versus SyntaxQ
IsExpression versus SyntaxQ
Direct comparisons
Direct comparisons
We have chosen a highly restricted 3 - element character set with which to form strings . This set is can in principle form all possible s combinators.
In[]:=
dirtest@nval_Integer:=Module[{allchars,q0,q1,q2},allchars={"s","[","]"};q0=StringJoin/@Tuples[allchars,nval];q1=Grid[Transpose@{Range@Length@q0,q0,IsExpression/@q0,SyntaxQ/@q0},Frame->All,AlignmentLeft];q1/.{a_,b_,c_,True}:>{a,b,c,True,FullForm@ToExpression@b}]
Discrepancies
Discrepancies
In[]:=
discreps@nval_Integer:=Module[{q1,q2,q3},q1=(dirtest@nval)[[1]];q2=Cases[q1,{___,True,False,___}];q3=Cases[q1,{___,False,True,___}];Grid[Union[q2,q3],Frame->All,AlignmentLeft]]
Discrepancy tables as nval ranges from 1 through 6
Discrepancy tables as nval ranges from 1 through 6
In[]:=
Grid[Transpose@{Range@6,discreps/@Range@6},Frame->All,AlignmentLeft]
Out[]=
1 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
4 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
5 |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
6 |
|
IsExpression is clearly on the right track, but far from perfect .
