Consider the following definition of splitList
:
elem[list_List, i_Integer] := list[[i]]
splitList[list_, size_Integer] := Table[elem[list, i], {i, size}]
Example use is like splitList[f[x], 3]
to get a list of 3 symbolic expressions, which would expand to f[x][[1]]
, f[x][[2]]
and f[x][[3]]
when x
attains such a value as to make f[x]
a true List
. I can't use literal list[[i]]
, since f[x]
is not a list initially, so I'd get Part::partd
error.
Here elem
is a function used only once, so it'd be good to avoid even naming it. Naturally, I thought I'd use a pure function for this. But then I come across a problem: my naïve approach doesn't work: the following code
splitList[list_, size_Integer] := Table[Function[{x_List,i_Integer},list[[i]] ], {i, size}]
emits an error message:
Function::flpar: Parameter specification {x_List,i_Integer} in Function[{x_List,i_Integer},f[x][[i]]] should be a symbol or a list of symbols.
So, apparently, I can't use patterns in Function
. How can I constrain parameters of a pure function then?
$Failed
orFailure
object, throw an exception, abort the surroundingTable
call or loop, etc). $\endgroup$Part::partd
errors onlist[[1]]
and siblings, and if the types don't match, the behavior should be to leave the expression unevaluated. Basically, I need the functionality of the first snippet, but without naming theelem
function. $\endgroup$elem
function carries certain semantics for what heppens in between evaluations: wheneverlist
is not a list, it preserves the call unevaluated. So it is used in more than one evaluation, separated by some time interval (the second one is whenlist
actually becomes a list, thus allowing it to evaluate). So this strategy of replacing withFunction
does not seem applicable:Function
always evaluates. $\endgroup$