Is it possible to use a previously defined value in subsequent definitions of a Block
?
According to syntax coloration, this should work as expected, that is, returning both the data list and its length:
Clear["data", "len"]
Block[{
data = Range[5],
len = Length[data]
},{len, data}
]
However, the result is:
{0,{1,2,3,4,5}}
Surprisingly enough, this works:
Clear["data", "sum"]
echo = Block[{
data = Range[5],
sum = Plus[data, 1]
}, {sum, data}
]
Producing:
{{2, 3, 4, 5, 6}, {1, 2, 3, 4, 5}}
Two questions:
- Why is
Length
unable to see the definition ofdata
--whereasPlus
seems to have no trouble with that? - Most importantly, how can I ensure any given expression will make use of the proper
data
value?
FWIW, I suspect this should have to do with Block
variables not being available immediately (as opposed to letrec
construct commonly found in functional language). If I'm right this could be fixed using Hold*
-related functions. But I was unable to find a solution by myself.