While e.g., letters[5]
(or any other integer between 1 and 22) is a list, letters
is not. Colloquially, letters
is a function, which is implementing in Mathematica as a symbol with DownValues
.
You can think of DownValues as rules which are applied during the sequence of evaluation, so that letters[5]
evaluates to {"α", "β", "γ", "δ", "ε"}
.
In fact, you can query the DownValues of letters
with fairly readable results:
DownValues[ letters ]
{ HoldPattern[letters[n_]] :> CharacterRange["α", "φ"][[1 ;; n]] }
(HoldPattern
is just a way of making sure the left hand side of the rule doesn't evaluate before the rule can be applied.)
Your problem was two-fold:
- No subpart of
letters[[3]]
is of the form letters[n_]
, so the DownValue couldn't apply
- More noticeably,
Part
immediately tries to evaluate, even if its first argument has insufficient length/depth, which results in your error.
If you'd like to use the syntax letters[[3]]
to return "α"
, you can instead redefine letters:
ClearAll[ letters ] (* Remove DownValues *)
letters = CharacterRange["α", "φ"];
letters
{"α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "ς", "σ", "τ", "υ", "φ"}
letters[[3]]
"γ"
Fun fact: while SetDelayed
(:=
) statements are stored as rules in DownValues
, Set
(=
) statements are stored in OwnValues
:
OwnValues[ letters ] (* after using `=` rather than `:=` *)
{ HoldPattern[letters] :> {"α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "ς", "σ", "τ", "υ", "φ"} }
n=4; Part[letters[n], 3]
$\endgroup$