Getting the type of Ans
Each of these routines can distinguish between two or more possible types contained in
Ans. The type is printed, leaving
Ans unchanged; this can be changed to a non-modifying variable write using, say,
For( or
IS>(. If an unsupported type is passed, expect errors.
This post was originally initiated by my discovery of List vs. String using attached formulas. The rest of the routines are assembled from the community and my own attempts to fill out the table of distinguishable types over the past 24 hours. Notably, since
toString( fails on strings (go figure), generic strings cannot be distinguished from the other types.
Number vs. List or String (by bfr)
Code: DelVar AFor(B,1,1
Ans->A:Ans->B
If A=B:Then
Disp "NUMBER"
Else
Disp "LIST OR STRING"
Number vs. List vs. (Evaluatable) String
If Ans is a string, it must evaluate to a list. If Ans is a list with only one unique value, it cannot be distinguished from a string (and vice-versa).
Code: DelVar AFor(B,1,1
Ans->A:Ans->B
If A=B:Then
Disp "NUMBER"
Else
If max(|LA)=min(|LA:Then
Disp "LIST OR STRING"
Else
SortA(|LA:SortD(|LB
If max(|LA!=|LB:Then
Disp "LIST"
Else
Disp "STRING"
Number vs. List vs. Matrix (CE or newer)
This routine requires toString(, which is only available on the CE or newer.
Code: If "["=sub(toString(Ans),1,1:Then
Disp "MATRIX"
Else
If "{"=sub(toString(Ans),1,1:Then
Disp "LIST"
Else
Disp "NUMBER"
List vs. Matrix (Destroys Ans)
Code: DelVar AFor(B,1,1
dim(Ans->A:Ans->B
If A=B:Then
Disp "LIST"
Else
Disp "MATRIX"
List vs. (Non-square) Matrix
If Ans is a square matrix, it cannot be distinguished from a list.
Code: If max(ΔList({1,1}dim(Ans:Then
Disp "NON-SQUARE MATRIX"
Else
Disp "LIST OR SQUARE MATRIX"
List vs. Matrix (small-ish dimension)
The number of elements in Ans must be less than 500 (if a list) or less than 200 (if a matrix).
Code: If max(ΔList({1,1}dim(Ans))+ΔList({1,1}dim(augment(Ans,Ans:Then
Disp "MATRIX"
Else
Disp "LIST"
Ways to fill in the missing comparisons (e.g. String vs. Matrix) are more than welcome.