Quantcast
Channel: Behaviour of `ToExpression` inside a function definition - Mathematica Stack Exchange
Viewing all articles
Browse latest Browse all 2

Answer by Nasser for Behaviour of `ToExpression` inside a function definition

$
0
0

It is because "arr" inside string do not bind to the input argument. You have to do it separatly.

a = {{1, 2}, {3, 4}};f[arr_] := ToExpression[ToString[arr] <> "[[1,1]]"]

And now it works

Mathematica graphics

But this type of coding can easily lead to errors and is fragile. It is better to find different approach for what you are trying to do.

takes in an array of unknown size

There is a command in Mathematica to find the size of list, it is called Dimensions and this way the called function can find the size of the input.


Viewing all articles
Browse latest Browse all 2

Trending Articles