Dynamic variable names: Difference between revisions

add E example
(Slate implementation)
(add E example)
Line 50:
 
* Common Lisp, by default, is case-insensitive; however it accomplishes this by canonicalizing read input to uppercase; there is syntax to denote a lower or mixed-case symbol name, <code>|Foo|</code> or <code>F\o\o</code>. <code>intern</code> does not go through the input path (''reader''), so we must provide the name in uppercase to make an "ordinary" variable name.
 
=={{header|E}}==
 
In E, there are no global variables, and there is no modification of the local (lexical) environment. However, it is possible to construct a program which binds any given variable name.
 
<lang e>def makeNounExpr := <elang:evm.makeNounExpr>
 
def dynVarName(name) {
def variable := makeNounExpr(null, name, null)
 
return e`{
def a := 1
def b := 2
def c := 3
{
def $variable := "BOO!"
[a, b, c]
}
}`.eval(safeScope)
}
 
? dynVarName("foo")
# value: [1, 2, 3]
 
? dynVarName("b")
# value: [1, "BOO!", 3]
 
? dynVarName("c")
# value: [1, 2, "BOO!"]</lang>
 
It is also possible to capture the environment object resulting from the evaluation of the constructed program and use it later; this is done by <code>bindX</code> in [[Eval in environment#E]] (except for the program being constant, which is independent).
 
=={{header|Forth}}==